Upgrade Hosted Agent / GitHub Runner PowerShell

I had recently fixed a bug in one of my build scripts by upgrading to the latest PowerShell Preview version. Of course, that version isn't yet available on the hosted agent for Azure Pipelines and GitHub Actions.

Upgrade Hosted Agent / GitHub Runner PowerShell

I managed to upgrade PowerShell at the start of the run and the agent will happily use it after installation.

You need to do two simple things.

  1. Install the PowerShell Core Preview onto the agent.
  2. Make PowerShell Core Preview the default by prepending the PATH environment variable.

Once you know how, it's easy:

- run: |
    Invoke-Expression "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -preview -quiet"
    "C:\Program Files\PowerShell\7-preview" >> $env:GITHUB_PATH
  name: Upgrade to latest preview of powershell 
  # https://github.com/PowerShell/PowerShell/issues/17404#issuecomment-1188348379

The same trick would work on Azure Pipelines:

- pwsh: | 
    Invoke-Expression "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -preview -quiet"
    write-host "##vso[task.prependpath]c:\Program Files\PowerShell\7-preview"