Getting started with PowerShellLoopsOperatorsUsing ShouldProcessPowerShell ClassesSwitch statementWorking with ObjectsUsing existing static classesBasic Set OperationsPowerShell FunctionsSending EmailHandling Secrets and CredentialsPowershell RemotingPowerShell "Streams"; Debug, Verbose, Warning, Error, Output and InformationVariables in PowerShellCommunicating with RESTful APIsWorking with the PowerShell pipelinePowerShell Background JobsReturn behavior in PowerShellWorking with XML FilesIntroduction to PsakeUsing the progress barStringsTCP Communication with PowerShellSharePoint ModuleAliasesAutomatic VariablesEnvironment VariablesPowershell profilesEnforcing script prerequisitesUsing the Help SystemSplattingDesired State ConfigurationSigning ScriptsSecurity and CryptographyCSV parsingIntroduction to PesterModules, Scripts and FunctionsPowerShell.exe Command-LineCommon parametersParameter setsRegular ExpressionsPowerShell Dynamic ParametersWMI and CIMGUI in PowershellConditional logicURL Encode/DecodeMongoDBRunning ExecutablesError handlingHashTablesActiveDirectory modulepowershell sql queriesAutomatic Variables - part 2Package managementCmdlet NamingBuilt-in variablesCreating DSC Class-Based ResourcesPowershell ModulesPowerShell WorkflowsHow to download latest artifact from Artifactory using Powershell script (v2.0 or below)?Calculated PropertiesSpecial OperatorsAnonymize IP (v4 and v6) in text file with PowershellComment-based helpAmazon Web Services (AWS) Simple Storage Service (S3)Amazon Web Services (AWS) RekognitionPSScriptAnalyzer - PowerShell Script AnalyzerNaming ConventionsEmbedding Managed Code (C# | VB)Archive ModuleInfrastructure AutomationScheduled tasks moduleISE module

PowerShell.exe Command-Line

Other topics

Executing a command

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

-Command <string>

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:

ParameterDescription
-Help | -? | /?Shows the help
-File <FilePath> [<Args>]Path to script-file that should be executed and arguments (optional)
-Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] }Commands to be executed followed by arguments
-EncodedCommand <Base64EncodedCommand>Base64 encoded commands
-ExecutionPolicy <ExecutionPolicy>Sets the execution policy for this process only
-InputFormat { Text | XML}Sets input format for data sent to process. Text (strings) or XML (serialized CLIXML)
-MtaPowerShell 3.0+: Runs PowerShell in multi-threaded apartment (STA is default)
-StaPowerShell 2.0: Runs PowerShell in a single-threaded apartment (MTA is default)
-NoExitLeaves PowerShell console running after executing the script/command
-NoLogoHides copyright-banner at launch
-NonInteractiveHides console from user
-NoProfileAvoid loading of PowerShell profiles for machine or user
-OutputFormat { Text | XML }Sets output format for data returned from PowerShell. Text (strings) or XML (serialized CLIXML)
-PSConsoleFile <FilePath>Loads a pre-created console file that configures the environment (created using Export-Console)
-Version <Windows PowerShell version>Specify a version of PowerShell to run. Mostly used with 2.0
-WindowStyle <style>Specifies whether to start the PowerShell process as a normal, hidden, minimized or maximized window.

Contributors

Topic Id: 5839

Example Ids: 20572,20573

This site is not affiliated with any of the contributors.