Script
suggest changeA script is a text file with the file extension .ps1
that contains PowerShell commands that will be executed when the script is called. Because scripts are saved files, they are easy to transfer between computers.
Scripts are often written to solve a specific problem, ex.:
- Run a weekly maintenance task
- To install and configure a solution/application on a computer
Demo
MyFirstScript.ps1:
Write-Host "Hello World!"
2+2
You can run a script by entering the path to the file using an:
- Absolute path, ex.
c:\MyFirstScript.ps1
- Relative path, ex
.\MyFirstScript.ps1
if the current directory of your PowerShell console wasC:\
Usage:
> .\MyFirstScript.ps1
Hello World!
4
A script can also import modules, define it’s own functions etc.
MySecondScript.ps1:
function HelloWorld {
Write-Host "Greetings from PowerShell!"
}
HelloWorld
Write-Host "Let's get started!"
2+2
HelloWorld
Usage:
> .\MySecondScript.ps1
Greetings from PowerShell!
Let's get started!
4
Greetings from PowerShell!
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents