Launch a powershell session.
Syntax
powershell[.exe] [-PSConsoleFile file | -Version version]
[-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive]
[-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
[-WindowStyle Style] [EncodedCommand Base64EncodedCommand]
[-File FilePath Args] [-ExecutionPolicy ExecutionPolicy]
[-Command { - | script-block [-args arg-array]
| string [CommandParameters] } ]
powershell[.exe] -Help | -? | /?
key
-PSConsole File Load a PowerShell console file. (created with export-console)
-Version Start the specified version of Windows PowerShell.
-NoLogo Hide the copyright banner at startup.
-NoExit Do not exit after running startup commands.
-Sta Start the shell using a single threaded apartment
-NoProfile Do not use the user profile.
-Noninteractive Don't present an interactive prompt to the user.
-InputFormat Format of data sent to Windows PowerShell. Valid values are
"Text" (string) or "XML" (serialized CLIXML format).
-OutputFormat Format the output. Valid values are "Text" (string)
or "XML" (serialized CLIXML format).
-Command Execute commands or a script file of commands
If Command is "-", the command text is read from standard input.
-WindowStyle Set the window style to Normal, Minimised, Maximised or Hidden.
-EncodedCommand Accepts a base-64 encoded string version of a command, Use this to
submit commands to PowerShell that require complex quotation marks
or curly braces.
-File Execute a script file.
-ExecutionPolicy Set the default execution policy for the session.
-Command Execute the specified commands (and any parameters) as though they
were typed at the PowerShell prompt, and then exit, unless NoExit is specified.
The value of Command can be "-", a string. or a script block.
If the value of Command is "-", the command is read from standard input.
Script blocks must be enclosed in braces ({}).
Specify a script block only when running PowerShell.exe from PowerShell.
The results of the script are returned to the parent shell as
deserialized XML objects, not live objects.
If the value of Command is a string, it must be the last parameter
in the command , any characters typed after command are interpreted
as the command arguments.
To write a string that runs a PowerShell command, use the format:
"& {command}"
where the quotation marks indicate a string and the call operator (&)
causes the command to be executed.
-Help, -?, /? Display Help
When launching a .ps1 script it is often a good idea to specify -noprofile to make sure that the script runs in a default PowerShell environment and does not load any profile scripts.
Examples
Load a console and run a Script:
PowerShell.exe -PSConsoleFile "C:\scripting\MyShell.psc1" -Command ". 'MyScript.ps1'"
Run a Script as Admin:
powershell -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file MyScript.ps1' -verb RunAs}"
Run a command to display the security event log:
powershell -command {get-eventlog -logname security}
Or the same thing but calling powershell from the CMD shell:
powershell -command "& {get-eventlog -logname security}"
PS.cmd - a simple batch file to launch PowerShell with less typing:
@echo off
Powershell.exe %1
Exit Codes
In PowerShell the exit code is stored in the automatic variable $LASTEXITCODE.
To read exit codes (other than 0 or 1) launch the PowerShell script and return the $LASTEXITCODE in a single line like this:
powershell.exe -noprofile C:\scripts\script.ps1; exit $LASTEXITCODE
Related:
List of all powershell commands
Equivalent bash command: bash - launch bash shell