PowerShell.exe Command-Line
Executing a command
Section titled “Executing a command”The -Command parameter is used to specify commands to be executed on launch. It supports multiple data inputs.
-Command
Section titled “-Command ”You can specify commands to executed on launch as a string. Multiple semicolon ;-separated statements may be executed.
>PowerShell.exe -Command "(Get-Date).ToShortDateString()"10.09.2016
>PowerShell.exe -Command "(Get-Date).ToShortDateString(); 'PowerShell is fun!'"10.09.2016PowerShell is fun!-Command { scriptblock }
Section titled “-Command { scriptblock }”The -Command parameter also supports a scriptblock input (one or multiple statements wrapped in braces { #code }. This only works when calling PowerShell.exe from another Windows PowerShell-session.
PS > powershell.exe -Command {"This can be useful, sometimes..."(Get-Date).ToShortDateString()}This can be useful, sometimes...10.09.2016-Command - (standard input)
Section titled “-Command - (standard input)”You can pass in commands from the standard input by using -Command -. The standard input can come from echo, reading a file, a legacy console application etc.
>echo "Hello World";"Greetings from PowerShell" | PowerShell.exe -NoProfile -Command -Hello WorldGreetings from PowerShellExecuting a script file
Section titled “Executing a script file”You can specify a file to a ps1-script to execute it’s content on launch using the -File parameter.
Basic script
Section titled “Basic script”MyScript.ps1
(Get-Date).ToShortDateString()"Hello World"Output:
>PowerShell.exe -File Desktop\MyScript.ps110.09.2016Hello WorldUsing parameters and arguments
Section titled “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 fooHello StackOverflow! Today's date it 10.09.2016First arg: fooParameters
Section titled “Parameters”|Parameter|Description
|---|---|---|---|---|---|---|---|---|---
|-Help | -? | /?|Shows the help
|-File Export-Console)
|-Version 2.0
|-WindowStyle , hidden, minimized or maximized window.