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, for example:
- 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, for example
C:\MyFirstScript.ps1 - Relative path, for example
.\MyFirstScript.ps1when the current location isC:\
Usage:
> .\MyFirstScript.ps1
Hello World!
4
A script can also import modules, define its own functions, and so on.
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