To reference a variable from the given context (env in this case) in the GitHub Actions workflow we have to use a dot (.) character, but you used a colon (:). To fix the error above the workflow should be adjusted:

- name: Checkout repo  
  uses: actions/checkout@v2
  with:
    repository: 'MyOrg/MyRepo'
    ref: ${{env.CURRENT_BRANCH}}

Additionally, you don't have to detect the current branch on and pass it to the checkout action. actions/checkout@v2 will use the current branch by default. So you only have to have:

- name: Checkout repo  
  uses: actions/checkout@v2
- name: Next Step
[...]
Answer from Marcin Kłopotek on Stack Overflow
Discussions

Set Environment Variable using Powershell
Could someone help me to set env variable using powershell ? I tried with ... >> $Env:GITHUB_ENV and ... | Out-File -FilePath $env:GITHUB_ENV -Append But it’s not show up when I do ${{ env.VA... More on github.com
🌐 github.com
3
3
January 27, 2022
How to use github variables with powershell 7?
Try this in the last line: run: gci ${{ GITHUB_WORKSPACE }} More on reddit.com
🌐 r/github
7
20
March 29, 2021
powershell - Setting environment variable value from .ps1 script not working in Github Actions - Stack Overflow
I have two ps1 scripts in Github Actions. My scenario: The first script executes before build Project builds The second script executes after build. I need to set the value inside the first script... More on stackoverflow.com
🌐 stackoverflow.com
Environment variables value pass to powershell script in prep command
Since my monitors setup is a bit complex, e.g. 2 external monitors(through a KVM switch, so these monitors might connect to another PC sometimes) + 1 pikvm + 1 emulated monitor, I need a program so... More on github.com
🌐 github.com
0
1
🌐
GitHub
github.com › orgs › community › discussions › 48791
Pass env. variable into a checkout powershell script · community · Discussion #48791
This will set an environment variable YOUR_VAR with the given value for the step. Beta Was this translation helpful? Give feedback. ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... This Discussion has been labeled as dormant by an automated system for having no activity in the last 60 days. Please consider one the following actions:
🌐
James Croft
jamescroft.co.uk › home › blog › setting github actions environment variables in powershell
Setting GitHub Actions environment variables in PowerShell - James Croft
June 24, 2022 - Afterward, the variable is appended to the GITHUB_ENV variables for you to use in the rest of your GitHub Actions workflow. Here’s the line to copy into your workflow. echo "MY_VARIABLE=hello" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append · You can then use the environment variable as you do ones set manually in your workflow, for example, $MY_VARIABLE. GitHub’s documentation contains an example of how you can also achieve this using other scripting languages.
🌐
Techwatching
techwatching.dev › posts › github-actions-var-and-context
4 tips about GitHub Actions environment variables and contexts - Alexandre Nédélec
September 6, 2021 - For instance, if you are on a Ubuntu GitHub-hosted runner, by default the shell will be bash whereas on a Windows GitHub-hosted runner it will be PowerShell (I think we don't say PowerShell Core anymore but I am speaking of pwsh, not the old Windows PowerShell of course). This is really important to know because depending on the shell used, the syntax to use an environment variable in a script is different as you can see on the documentation screenshot below:
🌐
TechTarget
techtarget.com › searchitoperations › tutorial › Automate-workflows-by-running-PowerShell-in-GitHub-Actions
Automate workflows by running PowerShell in GitHub Actions | TechTarget
This example uses the env: parameter to pass the secrets as environment variables to the PowerShell script. However, you could also store your script in a PS1 file and call that file instead of storing your code inline.
Published   June 30, 2023
Find elsewhere
Top answer
1 of 3
3

set-env was depricated - please check GitHub Actions: Deprecating set-env and add-path commands

As a replacement you may use

echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV

and then:

jobs:
  show:
    runs-on: ubuntu-latest
    steps:
      - name: Is variable exported?
        run: |
          echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
      - name: PowerShell script
        # You may pin to the exact commit or the version.
        # uses: Amadevus/pwsh-script@25a636480c7bc678a60bbf4e3e5ac03aca6cf2cd
        uses: Amadevus/[email protected]
        continue-on-error: true
        with: 
          # PowerShell script to execute in Actions-hydrated context
          script: | 
            Write-Host $env:BUILD_NUMBER
      - name: Read exported variable
        run: |
          echo "${{ env.BUILD_NUMBER}}"
2 of 3
2

To set environment variables in a step that can be referenced in another, you will need to use the ::set-env syntax.

In your case, your first script will have to run this command:

Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"

And the second script should be able to reference it with $env:BUILD_NUMBER.


[6/20/20] Update with full example.

Action yaml file (Inline powershell will have similar behavior than with a ps1):

name: StackOverFlow

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: windows-latest

    steps:
    - run: | 
        $buildNumber = "12345"
        Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"

    - run: Write-Output "Doing something else..."
      
    - run: Write-Output "The build number is $env:BUILD_NUMBER"

Output logs:

2020-06-20T23:13:23.3209811Z ##[section]Starting: Request a runner to run this job
2020-06-20T23:13:23.5144969Z Can't find any online and idle self-hosted runner in current repository that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145013Z Can't find any online and idle self-hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145038Z Found online and idle hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.6348644Z ##[section]Finishing: Request a runner to run this job
2020-06-20T23:13:29.9867339Z Current runner version: '2.263.0'
2020-06-20T23:13:29.9982614Z ##[group]Operating System
2020-06-20T23:13:29.9983190Z Microsoft Windows Server 2019
2020-06-20T23:13:29.9983380Z 10.0.17763
2020-06-20T23:13:29.9983515Z Datacenter
2020-06-20T23:13:29.9983691Z ##[endgroup]
2020-06-20T23:13:29.9983875Z ##[group]Virtual Environment
2020-06-20T23:13:29.9984067Z Environment: windows-2019
2020-06-20T23:13:29.9984247Z Version: 20200608.1
2020-06-20T23:13:29.9984524Z Included Software: https://github.com/actions/virtual-environments/blob/win19/20200608.1/images/win/Windows2019-Readme.md
2020-06-20T23:13:29.9984752Z ##[endgroup]
2020-06-20T23:13:29.9985890Z Prepare workflow directory
2020-06-20T23:13:30.0151643Z Prepare all required actions
2020-06-20T23:13:30.9154166Z ##[group]Run $buildNumber = "12345"
2020-06-20T23:13:30.9154566Z [36;1m$buildNumber = "12345"[0m
2020-06-20T23:13:30.9154784Z [36;1mWrite-Output "::set-env name=BUILD_NUMBER::$buildNumber"[0m
2020-06-20T23:13:30.9820753Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:30.9821156Z ##[endgroup]
2020-06-20T23:13:43.2981407Z ##[group]Run Write-Output "Doing something else..."
2020-06-20T23:13:43.2981812Z [36;1mWrite-Output "Doing something else..."[0m
2020-06-20T23:13:43.3022226Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.3022501Z env:
2020-06-20T23:13:43.3022706Z   BUILD_NUMBER: 12345
2020-06-20T23:13:43.3022906Z ##[endgroup]
2020-06-20T23:13:43.8091340Z Doing something else...
2020-06-20T23:13:43.8671648Z ##[group]Run Write-Output "The build number is $env:BUILD_NUMBER"
2020-06-20T23:13:43.8671986Z [36;1mWrite-Output "The build number is $($env:BUILD_NUMBER)"[0m
2020-06-20T23:13:43.8717102Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.8717288Z env:
2020-06-20T23:13:43.8718175Z   BUILD_NUMBER: 12345
2020-06-20T23:13:43.8718286Z ##[endgroup]
2020-06-20T23:13:44.4148124Z The build number is 12345
2020-06-20T23:13:44.4368449Z Cleaning up orphan processes
🌐
DEV Community
dev.to › robmulpeter › getting-started-with-github-action-workflows-3ehn
Getting Started with Github Action Workflows - DEV Community
February 17, 2022 - Run Get-Content ./Powershell/Variables.ps1 >> $Env:GITHUB_ENV Get-Content ./Powershell/Variables.ps1 >> $Env:GITHUB_ENV env: DOJO_1: Miyagi-Do Karate · The following example achieves the same outcome of example 4.2 but adds environment variables by executing a powershell script directly in the workflow step.
🌐
Jabbermouth
jabbermouth.co.uk › blog › 2025 › 01 › 05 › output-variables-in-powershell-and-github-actions
Output variables in PowerShell and GitHub Actions - Neil's Whiteboard
January 5, 2025 - Write-Host "Some value: $someValue" Write-Host "Another value: $anotherValue" "SOME_VALUE=$someValue" | Out-File -FilePath $env:GITHUB_OUTPUT -Append "ANOTHER_VALUE=$anotherValue" | Out-File -FilePath $env:GITHUB_OUTPUT -Append - name: Use the values shell: pwsh env: VALUE_TO_USE_1: ${{ steps.calculate_values.outputs.SOME_VALUE }} VALUE_TO_USE_2: ${{ steps.calculate_values.outputs.ANOTHER_VALUE }} run: | $ErrorActionPreference = "Stop" # not required but I always include it so errors don't get missed Write-Host "Values received were `"$($env:VALUE_TO_USE_1)`" and `"$($env:VALUE_TO_USE_2)`"" Whilst both examples use PowerShell, there is no requirement that both steps use the same shell.
🌐
GitHub
github.com › orgs › LizardByte › discussions › 496
Environment variables value pass to powershell script in prep command · LizardByte · Discussion #496
Yes, by adding cmd /c, the env vars actual values are passed correctly. Now it's like this: cmd /c powershell -ExecutionPolicy Bypass -File E:\Apps\Sunshine\MonitorStreaming.ps1 -action start -width "%SUNSHINE_CLIENT_WIDTH%" -height ...
Top answer
1 of 4
497

The original answer to this question used the Actions runner function set-env. Due to a security vulnerability set-env is being deprecated and should no longer be used.

This is the new way to set environment variables.

name: my workflow
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set env
      run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
    - name: Test
      run: echo $GITHUB_SHA_SHORT

Setting an environment variable echo "{name}={value}" >> $GITHUB_ENV

Creates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.

(From https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable)

Example using the output to $GITHUB_ENV method:

    echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV

This is an alternative way to reference the environment variable in workflows.

    - name: Test
      run: echo ${{ env.GITHUB_SHA_SHORT }}
2 of 4
27
  - name: Set and Retrieve Github ENV variables
    shell: bash
    run: |

      # define variables      
      tests=16
      failures=2

      # set them as GitHub ENV variables
      echo "Tests=$tests" >> $GITHUB_ENV
      echo "Failures=$failures" >> $GITHUB_ENV

      # retrieve these GitHub ENV variables
      echo "${{ env.Failures }} out of ${{ env.Tests }} tests failed on CI"

Output

  2 out of 16 test failed on CI
🌐
Ivobeerens
ivobeerens.nl › blog › 2024 › 06 › pass-github-variable-to-packer-powershell-provisioner
Pass a GitHub Actions secret variable to a Packer PowerShell provider
In the Packer configuration, add the PowerShell provisioner, add an environment variable and point to location of the script that downloads the software. provisioner "powershell" { environment_vars = ["saskey=${var.PKR_VAR_blobsas}"] script ...
🌐
Dfinke
dfinke.github.io › powershell › 2018 › 12 › 20 › PowerShell-and-GitHub-Actions.html
PowerShell and GitHub Actions | Researching the optimal; implementing the practical
December 20, 2018 - The resolves key points to the action named Deploy to Azure, line 6. You can think of this as a “Depends On”. The Deploy to Azure action does three things, tells it where to find the files to kick things off, passes in the secrets (you setup in the GitHub Action UI), and three environment variables are setup. All four of these values come from where you set up the Azure Service Principal. You’ll see these being used in the entrypoint.ps1. Dockerfiles use a simple DSL which allows you to automate the steps you would normally manually take to create an image. ... (See line 1) Update apt-get (think PowerShell Update-Module) and install git (think PowerShell Install-Module)
🌐
GitHub
docs.github.com › en › actions › reference › workflows-and-actions › workflow-commands
Workflow commands for GitHub Actions - GitHub Docs
The STATE_processID variable is then exclusively available to the cleanup script running under the main action. This example runs in main and uses JavaScript to display the value assigned to the STATE_processID environment variable:
🌐
GitHub
docs.github.com › actions › learn-github-actions › variables
Store information in variables - GitHub Docs
Once the job is sent to the runner, the step is executed and the environment variable in the echo command is interpolated using the appropriate syntax ($env:NAME for PowerShell on Windows, and $NAME for bash and sh on Linux and macOS).
🌐
CopyProgramming
copyprogramming.com › howto › how-to-set-env-parameters-conditionally-in-github-actions
Actions: GitHub Actions: Setting Environment Parameters with Conditions ⚙️
April 30, 2023 - ... In Powershell, utilizing Accessing environment variables requires a env: prefix, which distinguishes it from bash. To access the ref in your script, you can use $env:GITHUB_REF . Instead of using the run property, you have the option to utilize the templating provided by GitHub Actions ...