Executing a script file

suggest change

Pass a .ps1 file to -File to run that script when powershell.exe starts.

Basic script

MyScript.ps1

(Get-Date).ToShortDateString()
"Hello World"
>PowerShell.exe -File Desktop\MyScript.ps1
10.09.2016
Hello World

Using parameters and arguments

You can pass parameters and extra arguments after the file path. Named parameters bind to the script's param block. Arguments that do not bind are left in $args.

MyScript.ps1

param($Name)

"Hello $Name! Today's date is $((Get-Date).ToShortDateString())"
"First arg: $($args[0])"
>PowerShell.exe -File Desktop\MyScript.ps1 -Name StackOverflow foo
Hello StackOverflow! Today's date is 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