DevOps | Scripts | Automation

Azure DevOpsPowershell

How to enable PowerShell core in Azure Pipeline?

Hello Guys,

Whoever is working with Azure DevOps Pipeline with PowerShell might have faced challenges running PowerShell core (PS 6+) commands in PowerShell tasks (Normal or Azure PowerShell).

Some people can get the below error during the pipeline execution for PS7.

Parameter set cannot be resolved using the specified named parameters.

This article explains how to enable Powershell core in Azure Pipeline. It is just simple one-click enablement but I will go through scenarios where you can get this error message and where you can avoid this error by enabling PS core or by selecting the proper VM image.

Scenario1: VM Image
With Windows Image

When you run Azure Pipeline on the Windows VM image, it is quite possible that the PS7 command won’t run because by default Windows operating system has PowerShell 5 built-in.

The below YML pipeline is running on the latest Windows OS, and it has a Powershell task that checks the PowerShell version and runs the PowerShell core command.

trigger:
- none

pool:
  vmImage: windows-latest

steps:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Checking PowerShell version
      $psversiontable

      # Running PowerShell core command to display random numbers between 1 to 10.
         
       1..10 | foreach-Object -parallel {
          $_
       }
      
     

Output:

PS7 output in Azure Pipeline

If you notice, in the output, I have marked 3 parts.

The first part shows that it is running powershell.exe, the second part shows it is using PowerShell 5.1 version (which is the PowerShell framework version) and the third part shows the error message produced while running the PowerShell core command because it couldn’t run on the PowerShell framework version.

With Ubuntu (Linux) Image:

Alright, now try the same thing with the Ubuntu image available for Azure Pipeline. In the below YAML file, we have replaced the windows image with the latest Ubuntu image.

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Checking PowerShell version
      $psversiontable

      # Running PowerShell core command to display random numbers between 1 to 10.
         
       1..10 | foreach-Object -parallel {
          $_
       }
      

Output:

Ubuntu Powershell Output

In the above image, we can see the PowerShell version on the Ubuntu latest version is 7.2.5. It is using the pwsh.exe process that is for the PowerShell core process and running the script successfully.

Scenario2: Enable PowerShell core in Image.

So from the scenario1 conclusion, we need to enable the PowerShell core for the Windows OS because it doesn’t have the default PS core version, unlike the ubuntu image.

To enable the PowerShell core, click on the Advanced tab if you are using the YAML pipeline-based editor.

Enable PowerShell core

Once you add the PowerShell core, the YAML file will add pwsh:true in your code.

trigger:
- none

pool:
  vmImage: windows-latest

steps:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Checking PowerShell version
      $psversiontable

      # Running PowerShell core command to display random numbers between 1 to 10.
         
       1..10 | foreach-Object -parallel {
          $_
       }
    pwsh: true

Output:

Windows Image with PS core

So it is successful and uses Powershell core latest version on the Windows OS. The same setting you can select for the ubuntu image but it has already the PowerShell core version on its latest operating system so enabling doesn’t matter.