# PowerShell.exe Command-Line

# Executing a command

The -Command parameter is used to specify commands to be executed on launch. It supports multiple data inputs.

# -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.2016
PowerShell is fun!

# -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)

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 World
Greetings from PowerShell

# 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

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

# Parameters

Parameter Description
-Help -?
-File [] Path to script-file that should be executed and arguments (optional)
-Command { - [-args ]
-EncodedCommand Base64 encoded commands
-ExecutionPolicy Sets the execution policy for this process only
-InputFormat { Text XML}
-Mta PowerShell 3.0+: Runs PowerShell in multi-threaded apartment (STA is default)
-Sta PowerShell 2.0: Runs PowerShell in a single-threaded apartment (MTA is default)
-NoExit Leaves PowerShell console running after executing the script/command
-NoLogo Hides copyright-banner at launch
-NonInteractive Hides console from user
-NoProfile Avoid loading of PowerShell profiles for machine or user
-OutputFormat { Text XML }
-PSConsoleFile Loads a pre-created console file that configures the environment (created using Export-Console)
-Version Specify a version of PowerShell to run. Mostly used with 2.0
-WindowStyle