Saturday 27 February 2021

DevOps Snack: Change PowerShell version in YAML

Case
I'm executing a PowerShell script via my DevOps YAML pipeline. How do I change the PowerShell version from 5 to 7 in de PowerShell@2 task?
YAML PowerShell Task












Solution
In your YAML code add the argument pwsh: true to the inputs (the default value is false). Now the script will be executed by pwsh.exe (PowerShell core) instead of powershell.exe (PowerShell 5). Changing the version only works on Windows agents, because only they can run both PowerShell 5 and PowerShell Core.
steps:
- task: PowerShell@2
  displayName: RunWithPs5
  inputs:
    pwsh: false
    filePath: '$(Pipeline.Workspace)\s\PowerShell\GetVersion.ps1'

- task: PowerShell@2
  displayName:  RunWithPs7
  inputs:
    pwsh: true
    filePath: '$(Pipeline.Workspace)\s\PowerShell\GetVersion.ps1'
#Show PowerShell version in PowerShell script GetVersion.ps1
Write-Host "PowerShell Version"
$PSVersionTable.PSVersion 
The Result
Now execute the pipeline and compare both PowerShell tasks. You see two signs that the PowerShell version has changed.
YAML PowerShell 5 and 7














Conclusion
An easy option to change the PowerShell executable from 5 to core. This is very handy if you need certain cmdlets that are only available in PowerShell Core like Test-Json.

No comments:

Post a Comment

All comments will be verified first to avoid URL spammers. यूआरएल स्पैमर से बचने के लिए सभी टिप्पणियों को पहले सत्यापित किया जाएगा।