Issuing workflow commands from the Windows shell in GitHub Actions

I don't think you should, I'm not sure you want to... But in case you need to... Here's how to correctly issue a GitHub Actions workflow command from the windows shell.

Issuing workflow commands from the Windows shell in GitHub Actions
Windows Shell

It's a little-known fact that the default shell in for GitHub Actions is different depending on the operating system on which you run. And that the syntax to set a variable, for example, differs too.

The docs explain the syntax for Bash and PowerShell:

Workflow commands for GitHub Actions - GitHub Docs
You can use workflow commands when running shell commands in a workflow or in an action’s code.

Below is an example of the different syntax required to set an output variable in each of the most common shells:

jobs:
  windows:
    runs-on: windows-latest
    steps:
    # Runs on PowerShell Core
    - name: Set an output variable on Windows
      id: set-value
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT
        
  linux:
    runs-on: ubuntu-latest
    steps:
    # Runs on Bash
    - name: Set an output variable on Linux
      id: set-value
      run: |
        echo foo=bar >> $GITHUB_OUTPUT
     

When you always target the same OS, it won't matter much, but if you have multi-platform jobs, matrix jobs, reusable templates, and composite actions, it pays to be explicit. That way your bash script won't suddenly be executed by PowerShell or vice versa.

You aren't limited to pwsh on windows or bash on linux and mac either. As long as they are installed on the Runner, you can run either on each OS.

jobs:
  windows:
    runs-on: windows-latest
    steps:
    # Runs on Bash
    - name: Set an output variable on Windows
      id: set-value-bash
      run: |
        echo foo=bar >> $GITHUB_OUTPUT
      shell: bash
    - name: Set an output variable on Windows
      id: set-value-pwsh
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT
      shell: pwsh
    - name: Set an output variable on Window
      id: set-value-powershell
      # Requires explicitly setting the output encoding
      run: |
        "foo=bar" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
      shell: powershell

        
  linux:
    runs-on: ubuntu-latest
    steps:
    # Runs on Bash
    - name: Set an output variable on Linux
      id: set-value-bash
      run: |
        echo foo=bar >> $GITHUB_OUTPUT
    - name: Set an output variable on Linux
      id: set-value-pwsh
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT
      shell: pwsh

Today someone asked what the correct syntax would be to do this on the shell: cmd old Windows Command Shell. I couldn't find that in the docs.

To be honest, since I wrote half of that doc, I was certain I didn't add that information...

But after some experimentation I found out you need to do the same things you'd do for the old PowerShell. Set the encoding to utf-8 and use the correct environment variable syntax:

jobs:
  windows:
    runs-on: windows-latest
    steps:
    - shell: cmd
      id: set-value-cmd
      run: |
          @chcp 65001>nul
          echo foo=bar >> %GITHUB_OUTPUT%
    jobs:

    - shell: cmd
      env:
        foo: ${{ steps.set-value-cmd.outputs.foo }}
      run: |
          @chcp 65001>nul
          echo %FOO%

First published here.