Troubleshooting "The term 'pwsh.exe' is not recognized" Error in Azure PowerShell@5 Task
This article delves into a common issue encountered while using the Azure PowerShell@5 task in Azure DevOps pipelines: "The term 'pwsh.exe' is not recognized as the name of a cmdlet, function, script file, or operable program." This error indicates that the pipeline cannot locate the PowerShell executable (pwsh.exe
) required to run your scripts.
Scenario:
You're working on an Azure DevOps pipeline that utilizes the Azure PowerShell@5 task to execute PowerShell scripts. During the pipeline execution, you encounter the error "The term 'pwsh.exe' is not recognized as the name of a cmdlet, function, script file, or operable program."
Original Code:
jobs:
- job: 'MyJob'
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzurePowerShell@5
displayName: 'Execute PowerShell script'
inputs:
azureSubscription: 'MyAzureSubscription'
ScriptPath: 'path/to/my/script.ps1'
Analysis:
This error typically arises from one of the following reasons:
- Missing PowerShell Runtime: The most common reason is that the agent you're using doesn't have PowerShell installed. While some agent images might have it pre-installed, others may not.
- Incorrect Path Configuration: The pipeline might not have the correct path to the PowerShell executable (
pwsh.exe
) in its environment variables. - Incorrect Task Configuration: The Azure PowerShell@5 task may be configured with an incorrect version of PowerShell, leading to incompatibility.
Solutions:
-
Install PowerShell on the Agent: If the agent lacks PowerShell, install it by adding a pre-build step in your pipeline:
jobs: - job: 'MyJob' pool: vmImage: 'ubuntu-latest' steps: - task: PowerShell@2 displayName: 'Install PowerShell' inputs: targetType: 'inlineScript' script: | Install-PackageProvider -Name NuGet -Force Install-Module -Name PowerShellGet -Force Install-Module -Name AzureRM Install-Module -Name Az Install-Module -Name Az.Accounts - task: AzurePowerShell@5 displayName: 'Execute PowerShell script' inputs: azureSubscription: 'MyAzureSubscription' ScriptPath: 'path/to/my/script.ps1'
-
Configure PowerShell Path: Verify that the path to the PowerShell executable is correctly set in the agent's environment variables. You can do this by adding a
powershell
task to print the path:jobs: - job: 'MyJob' pool: vmImage: 'ubuntu-latest' steps: - task: PowerShell@2 displayName: 'Check PowerShell Path' inputs: targetType: 'inlineScript' script: | Write-Host $env:PATH
-
Validate Task Configuration: Double-check the Azure PowerShell@5 task settings to ensure you're using the appropriate version of PowerShell. You might need to use a specific version if your scripts rely on features not available in older versions.
Additional Tips:
- Clear Cache: Occasionally, cache issues can cause this problem. Try clearing the agent's cache to rule this out.
- Check Logs: Review the pipeline logs for additional clues about the error. You might find information about the missing executable or path issues.
- Use Inline Script: Consider using the
inlineScript
option within the Azure PowerShell@5 task to directly embed your script into the pipeline. This eliminates the need to manage external script files.
References:
- Azure PowerShell Task Documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops
- PowerShell Documentation: https://learn.microsoft.com/en-us/powershell/
By understanding the potential causes and implementing the appropriate solutions, you can resolve this error and execute your PowerShell scripts successfully within your Azure DevOps pipelines.