A 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.:
MyFirstScript.ps1:
Write-Host "Hello World!"
2+2
You can run a script by entering the path to the file using an:
c:\MyFirstScript.ps1
.\MyFirstScript.ps1
if the current directory of your PowerShell console was C:\
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!