Issuing workflow commands from the Windows shell in GitHub Actions
Tags: githubactions, github, windowsserver
Published: 2022-11-15 18:07:18

---
title: Issuing workflow commands from the Windows shell in GitHub Actions
published: true
date: 2022-11-15 17:54:12 UTC
tags: GitHubActions,GitHub,WindowsServer
canonical_url: https://jessehouwing.net/github-actions-workflow-commands-in-cmd/
---

 ![Issuing workflow commands from the Windows shell in GitHub Actions](https://jessehouwing.net/content/images/2022/11/shell.png)

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
](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions)

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
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT
        
  linux:
    runs-on: ubuntu-latest
    steps:
    # Runs on Bash
    - name: Set an output variable on Linux
      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
      run: |
        echo foo=bar >> $GITHUB_OUTPUT
      shell: bash
    - name: Set an output variable on Windows
      run: |
        echo foo=bar >> $env:GITHUB_OUTPUT
      shell: pwsh
    - name: Set an output variable on Windows
      # 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
      run: |
        echo foo=bar >> $GITHUB_OUTPUT
    - name: Set an output variable on Linux
      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
      run: |
        @chcp 65001>nul
        echo foo=bar >> %GITHUB_OUTPUT%
```

[First published here](https://stackoverflow.com/a/74444094/736079).