Azure PowerShell task

Azure DevOps Services

Use this task to run a PowerShell script within an Azure environment. The Azure context is authenticated with the provided Azure Resource Manager service connection.

YAML snippet

# Azure PowerShell
# Run a PowerShell script within an Azure environment
- task: AzurePowerShell@5
    #ConnectedServiceNameARM: Required. Name of Azure Resource Manager service connection. You can also use azureSubscription.
    #scriptType: 'FilePath' # Optional. Options: filePath, inlineScript
    #scriptPath: # Optional
    #inline: # Optional. You can write your Azure PowerShell scripts inline here. You can also pass predefined and custom variables to this script using arguments 
    #scriptArguments: # Optional
    #errorActionPreference: 'stop' # Optional. Options: stop, continue, silentlyContinue
    #failOnStandardError: false # Optional
    #TargetAzurePs: 'OtherVersion' # Required. Options: latestVersion, otherVersion. You can also use azurePowerShellVersion.
    #CustomTargetAzurePs: # Required when azurePowerShellVersion == OtherVersion. You can also use preferredAzurePowerShellVersion.
    #pwsh: true # Optional. If this is true, PowerShell Core pwsh.exe will be used in Windows environments.
    #workingDirectory: '' # Optional. The working directory where the script is run.


Argument Description
Azure Subscription
(Required) Name of an Azure Resource Manager service connection for authentication.
Alias: azureSubscription
Script Type
(Optional) Type of the script: filePath or inlineScript
Default value: FilePath.
Script Path
(Optional) Path of the script. Should be fully qualified path or relative to the default working directory.
Inline Script
(Optional) You can write your scripts inline here.
Script Arguments
(Optional) Additional parameters to pass to PowerShell. Can be either ordinal or named parameters. Not applicable for inline script option.
(Optional) Select the value of the ErrorActionPreference variable for executing the script.
Default value: stop. Options: stop, continue, silentlyContinue
Fail on Standard Error
(Optional) If this is true, this task will fail if any errors are written to the error pipeline, or if any data is written to the Standard Error stream.
Default value: false
Azure PowerShell Version
(Required) In case of Microsoft-hosted agents, the supported Azure PowerShell version. To pick the latest version available on the agent, select latestVersion
For self-hosted agents, you can specify preferred version of Azure PowerShell using "Specify version"
Default value: OtherVersion
Argument alias: azurePowerShellVersion
(Required when TargetAzurePs = OtherVersion)
Preferred Azure PowerShell Version needs to be a proper semantic version. For example, 1.2.3. Regex like 2.*,2.3.* is not supported.
Argument alias: preferredAzurePowerShellVersion
Use PowerShell Core
(Optional) If this is true, on Windows, the task will use pwsh.exe from your PATH instead of powershell.exe.
Working Directory
(Optional) Working directory where the script is run. Default value: ''.


- task: AzurePowerShell@5
    azureSubscription: my-arm-service-connection
    scriptType: filePath
    scriptPath: $(Build.SourcesDirectory)\myscript.ps1
      -Arg1 val1 `
      -Arg2 val2 `
      -Arg3 val3
    azurePowerShellVersion: latestVersion
    pwsh: true


Script worked locally, but failed in the pipeline

This typically occurs when the service connection used in the pipeline has insufficient permissions to run the script. Locally, the script runs with your credentials and would succeed as you may have the required access.

To resolve this issue, ensure the service principle/ authentication credentials have the required permissions. For more information, see Use Role-Based Access Control to manage access to your Azure subscription resources.

Error: Could not find the modules: '<module name>' with Version: '<version>'. If the module was recently installed, retry after restarting the Azure Pipelines task agent

Azure PowerShell task uses Azure/AzureRM/Az PowerShell Module to interact with Azure Subscription. This issue occurs when the PowerShell module is not available on the Hosted Agent. Hence, for a particular task version, Preferred Azure PowerShell version must be specified in the Azure PowerShell version options from the list of available versions. The installed software can be found in the Software table in Microsoft-hosted agents.

Service Connection Issues

To troubleshoot issues related to service connections, see Service Connection troubleshooting.

Open source

This task is open source on GitHub. Feedback and contributions are welcome.