| title | PowerShell@2 - PowerShell v2 task |
|---|---|
| description | Run a PowerShell script on Linux, macOS, or Windows. |
| ms.date | 04/27/2026 |
| monikerRange | =azure-pipelines || =azure-pipelines-server || =azure-pipelines-2022.2 || =azure-pipelines-2022.1 || =azure-pipelines-2022 |
| author | ramiMSFT |
| ms.author | rabououn |
:::moniker range="<=azure-pipelines"
Use this task to run a PowerShell script on Linux, macOS, or Windows.
Note
By default, PowerShell v2 uses PowerShell Core for Linux agents and Windows PowerShell for Windows agents. To use the latest version of PowerShell on Windows agents, set the pwsh parameter to true. PowerShell Core will then be used instead.
:::moniker-end
:::moniker range=">=azure-pipelines-2022.1"
# PowerShell v2
# Run a PowerShell script on Linux, macOS, or Windows.
- task: PowerShell@2
inputs:
#targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
filePath: # string. Required when targetType = filePath. Script Path.
#arguments: # string. Optional. Use when targetType = filePath. Arguments.
#script: # string. Required when targetType = inline. Script.
# Preference Variables
#errorActionPreference: 'stop' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
#warningPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. WarningPreference. Default: default.
#informationPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. InformationPreference. Default: default.
#verbosePreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. VerbosePreference. Default: default.
#debugPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. DebugPreference. Default: default.
#progressPreference: 'silentlyContinue' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. ProgressPreference. Default: silentlyContinue.
# Advanced
#failOnStderr: false # boolean. Fail on Standard Error. Default: false.
#showWarnings: false # boolean. Show warnings as Azure DevOps warnings. Default: false.
#ignoreLASTEXITCODE: false # boolean. Ignore $LASTEXITCODE. Default: false.
#pwsh: false # boolean. Use PowerShell Core. Default: false.
#workingDirectory: # string. Working Directory.
#runScriptInSeparateScope: false # boolean. Run script in the separate scope. Default: false.:::moniker-end
:::moniker range="=azure-pipelines-2022"
# PowerShell v2
# Run a PowerShell script on Linux, macOS, or Windows.
- task: PowerShell@2
inputs:
#targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
filePath: # string. Required when targetType = filePath. Script Path.
#arguments: # string. Optional. Use when targetType = filePath. Arguments.
#script: # string. Required when targetType = inline. Script.
# Preference Variables
#errorActionPreference: 'stop' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
#warningPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. WarningPreference. Default: default.
#informationPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. InformationPreference. Default: default.
#verbosePreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. VerbosePreference. Default: default.
#debugPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. DebugPreference. Default: default.
# Advanced
#failOnStderr: false # boolean. Fail on Standard Error. Default: false.
#showWarnings: false # boolean. Show warnings as Azure DevOps warnings. Default: false.
#ignoreLASTEXITCODE: false # boolean. Ignore $LASTEXITCODE. Default: false.
#pwsh: false # boolean. Use PowerShell Core. Default: false.
#workingDirectory: # string. Working Directory.
#runScriptInSeparateScope: false # boolean. Run script in the separate scope. Default: false.:::moniker-end
:::moniker range="<=azure-pipelines"
targetType - Type
string. Allowed values: filePath (File Path), inline. Default value: filePath.
Specifies the type of script for the task to run: an inline script or a path to a .ps1 file.
:::moniker-end
:::moniker range="<=azure-pipelines"
filePath - Script Path
string. Required when targetType = filePath.
Specifies the path of the script to execute. Must be a fully qualified path or relative to $(System.DefaultWorkingDirectory).
:::moniker-end
:::moniker range="<=azure-pipelines"
arguments - Arguments
string. Optional. Use when targetType = filePath.
Specifies the arguments passed to the PowerShell script. Arguments can be ordinal parameters or named parameters. For example, -Name someName -Path -Value "Some long string value".
arguments is not used when targetType is set to inline.
:::moniker-end
:::moniker range="<=azure-pipelines"
script - Script
string. Required when targetType = inline. Default value: # Write your PowerShell commands here.\n\nWrite-Host "Hello World".
Specifies the contents of the script. The maximum supported inline script length is 20000 characters. Use a script from a file if you want to use a longer script.
:::moniker-end
:::moniker range="<=azure-pipelines"
errorActionPreference - ErrorActionPreference
string. Allowed values: default, stop, continue, silentlyContinue. Default value: stop.
Prepends the line $ErrorActionPreference = 'VALUE' at the top of your script.
:::moniker-end
:::moniker range="<=azure-pipelines"
warningPreference - WarningPreference
string. Allowed values: default, stop, continue, silentlyContinue. Default value: default.
When not set to Default, prepends the line $WarningPreference = 'VALUE' at the top of your script.
:::moniker-end
:::moniker range="<=azure-pipelines"
informationPreference - InformationPreference
string. Allowed values: default, stop, continue, silentlyContinue. Default value: default.
When not set to Default, prepends the line $InformationPreference = 'VALUE' at the top of your script.
:::moniker-end
:::moniker range="<=azure-pipelines"
verbosePreference - VerbosePreference
string. Allowed values: default, stop, continue, silentlyContinue. Default value: default.
When not set to Default, prepends the line $VerbosePreference = 'VALUE' at the top of your script.
:::moniker-end
:::moniker range="<=azure-pipelines"
debugPreference - DebugPreference
string. Allowed values: default, stop, continue, silentlyContinue. Default value: default.
When not set to Default, prepends the line $DebugPreference = 'VALUE' at the top of your script.
:::moniker-end
:::moniker range=">=azure-pipelines-2022.1"
progressPreference - ProgressPreference
string. Allowed values: default, stop, continue, silentlyContinue. Default value: silentlyContinue.
When not set to Default, prepends the line $ProgressPreference = 'VALUE' at the top of your script.
:::moniker-end
:::moniker range="<=azure-pipelines"
failOnStderr - Fail on Standard Error
boolean. Default value: false.
If the value of this boolean is true, the task fails if any errors are written to the error pipeline or if any data is written to the Standard Error stream. Otherwise, the task relies on the exit code to determine failure.
:::moniker-end
:::moniker range="<=azure-pipelines"
showWarnings - Show warnings as Azure DevOps warnings
boolean. Default value: false.
If the value is set to true, and your script writes warnings, then the warnings will appear as warnings in Pipeline logs.
:::moniker-end
:::moniker range="<=azure-pipelines"
ignoreLASTEXITCODE - Ignore $LASTEXITCODE
boolean. Default value: false.
If the value is set to false, the line if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE } is appended to the end of your script. This will cause the last exit code from an external command to be propagated as the exit code of powershell. Otherwise, the line is not appended to the end of your script.
:::moniker-end
:::moniker range="<=azure-pipelines"
pwsh - Use PowerShell Core
boolean. Default value: false.
If this is true, then tasks running on Windows agents will use pwsh.exe from your path instead of powershell.exe.
:::moniker-end
:::moniker range="<=azure-pipelines"
workingDirectory - Working Directory
string.
Specifies the working directory where the script is run. If a value is not specified, the working directory is $(Build.SourcesDirectory).
:::moniker-end
:::moniker range="<=azure-pipelines"
runScriptInSeparateScope - Run script in the separate scope
boolean. Default value: false.
This input allows executing PowerShell scripts using & operator instead of the default .. If this input is set to true, the script will be executed in a separate scope, and globally scoped PowerShell variables won't be updated.
:::moniker-end
All tasks have control options in addition to their task inputs. For more information, see Control options and common task properties.
:::moniker range="<=azure-pipelines"
None.
:::moniker-end
Each PowerShell session lasts only for the duration of the job in which it runs. Tasks that depend on what has been bootstrapped must be in the same job as the bootstrap.
PowerShell@2 has two shortcuts in YAML: steps.powershell and steps.pwsh.
powershellruns using either Windows PowerShell (on Windows) orpwsh(Linux and macOS).pwshruns PowerShell Core, the cross-platform edition of PowerShell built on .NET Core.
steps:
- powershell: # Run a script in Windows PowerShell on Windows, and pwsh on Linux and macOS.
- pwsh: # Run a script in PowerShell Core on Windows, macOS, and Linux.To learn more about defining build variables in a script, see Define and modify your build variables in a script.
To learn more about defining release variables in a script, see Define and modify your release variables in a script.
Be aware that PowerShell cuts off error messages, so if you use pipeline secrets in a script, the secrets could be trimmed and exposed. For example, in the inline script below:
./script.ps1 --arg1 value1 --arg2 <some_secret_which_will_be_masked_here>There could be an exception like: At <path_to_temp_script_file>:4 char:3:
+ ./script.ps1 --arg1 value1 --arg2 <unmasked_part_of_original_secret> ...
+ ~~~~~~~~~~
+ <Additional exception details>To avoid this issue, you can handle these exceptions on a script level, or avoid cases when pipeline secrets could appear in source code lines within error messages.
- Invoke a script from a file
- Write a warning
- Write an error
- Call PowerShell script with multiple arguments
The following is a sample PowerShell file named test.ps1 located in the root of your repository.
Write-Host "Hello World from $Env:AGENT_NAME."
Write-Host "My ID is $Env:AGENT_ID."
Write-Host "AGENT_WORKFOLDER contents:"
gci $Env:AGENT_WORKFOLDER
Write-Host "AGENT_BUILDDIRECTORY contents:"
gci $Env:AGENT_BUILDDIRECTORY
Write-Host "BUILD_SOURCESDIRECTORY contents:"
gci $Env:BUILD_SOURCESDIRECTORY
Write-Host "Over and out."You can invoke this script in your pipeline like this.
steps:
- task: PowerShell@2
inputs:
targetType: 'filePath'
filePath: 'test.ps1'- task: PowerShell@2
inputs:
targetType: 'inline'
script: Write-Host "##vso[task.LogIssue type=warning;]This is the warning"
# Writes a warning to build summary and to log in yellow text- task: PowerShell@2
inputs:
targetType: 'inline'
script: Write-Host "##vso[task.LogIssue type=error;]This is the error"
# Writes an error to build summary and to log in red textIf you want this error to fail the build, add exit 1 to the script.
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Write-Host "##vso[task.LogIssue type=error;]This is the error"
exit 1
# Writes an error to build summary and to log in red textCreate PowerShell script test2.ps1:
param ($input1, $input2)
Write-Host "$input1 $input2"In your YAML pipeline, call:
- task: PowerShell@2
inputs:
targetType: 'filePath'
filePath: $(System.DefaultWorkingDirectory)\test2.ps1
arguments: > # Use this to avoid newline characters in multiline string
-input1 "Hello"
-input2 "World"
displayName: 'Print Hello World':::moniker range="<=azure-pipelines"
| Requirement | Description |
|---|---|
| Pipeline types | YAML, Classic build, Classic release |
| Runs on | Agent, DeploymentGroup |
| Demands | None |
| Capabilities | This task does not satisfy any demands for subsequent tasks in the job. |
| Command restrictions | Any |
| Settable variables | Any |
| Agent version | 2.115.0 or greater |
| Task category | Utility |
:::moniker-end