Executing a script file

suggest change

You can specify a file to a ps1-script to execute it’s content on launch using the -File parameter.

Basic script

MyScript.ps1

(Get-Date).ToShortDateString()
"Hello World"

Output:

>PowerShell.exe -File Desktop\MyScript.ps1
10.09.2016
Hello World

Using parameters and arguments

You can add parameters and/or arguments after filepath to use them in the script. Arguments will be used as values for undefined/available script-parameters, the rest will be available in the $args-array

MyScript.ps1

param($Name)

"Hello $Name! Today's date it $((Get-Date).ToShortDateString())"
"First arg: $($args[0])"

Output:

>PowerShell.exe -File Desktop\MyScript.ps1 -Name StackOverflow foo
Hello StackOverflow! Today's date it 10.09.2016
First arg: foo

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents