Solved it, apparently I dont need to use Write-Host or echo before >>
just do
"VAR=TEST" >> $env:GITHUB_ENV

🌐
GitHub
gist.github.com › mitchmindtree › 92c8e37fa80c8dddee5b94fc88d1288b
How to permanently set user environment variables on Windows · GitHub
Using the command prompt: setx FOO "path\to\foo" Using powershell: [Environment]::SetEnvironmentVariable("FOO", "path\to\foo", [System.EnvironmentVariableTarget]::User) Using the windows GUI.
Discussions

Setting Windows PowerShell environment variables - Stack Overflow
I have found out that setting the PATH environment variable affects only the old command prompt. PowerShell seems to have different environment settings. How do I change the environment variables for PowerShell (v1)? ... I want to make my changes permanent, so I don't have to set it every time ... More on stackoverflow.com
🌐 stackoverflow.com
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
Set a persistent environment variable on a windows 10 computer with PowerShell?
Check out this method: https://docs.microsoft.com/en-us/dotnet/api/system.environment.setenvironmentvariable?view=net-6.0#system-environment-setenvironmentvariable(system-string-system-string-system-environmentvariabletarget) More on reddit.com
🌐 r/PowerShell
12
3
February 3, 2022
[Environment]::SetEnvironmentVariable() fixes and improvements
Summary of the new feature / enhancement [Environment]::SetEnvironmentVariable("Variable", $null) should delete the environmentvariable and when passing [Environment]::SetEnvironmentVaria... More on github.com
🌐 github.com
6
January 26, 2026
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_environment_variables
about_Environment_Variables - PowerShell | Microsoft Learn
It lets you get, add, change, clear, and delete environment variables and values in PowerShell. For example, to create the Foo environment variable with a value of Bar: ... You can also copy the environment variable with Copy-Item, set the value of an environment variable with Set-Item, list environment variables with Get-Item, and delete the environment variable with Remove-Item.
🌐
TechTarget
techtarget.com › searchitoperations › answer › Manage-the-Windows-PATH-environment-variable-with-PowerShell
Manage the Windows PATH environment variable with PowerShell | TechTarget
If you need the changes to be permanent, there are two ways to handle that, depending on how global you want the changes. If you want to add a path to the PATH variable only for PowerShell and you want the change to be in PowerShell and only PowerShell, you can do this by adding a few lines to your PowerShell profile. To find your profile, simply type '$profile' into your PowerShell prompt. For instance, if you place the Set-PathVariable function at the top of your profile.ps1, you can now add a path using the following:
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-set-environment-variables-using-powershell
How to Set environment variables using PowerShell?
To add or set the environment variable ... need to use the .NET method. To set the environment persistently so they should remain even when close the session, PowerShell uses [System.Environment] class with the SetEnvironmentVariable method for the environment variable to set it persistently...
🌐
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 - 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.
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
🌐
GitHub
github.com › nickcox › set-env
GitHub - nickcox/set-env: Powershell module to set or unset environment variables in the context of a single command, Unix style. · GitHub
Install-Module set-env Import-Module set-env # add to profile. e.g: Add-Content $PROFILE `n, 'Import-Module set-env' Add-Content $PROFILE 'Register-AutoSetEnv' Add-Content $PROFILE 'New-Alias env set-env' Import the module and call ...
Author   nickcox
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › how to set environment variables in powershell?
How to Set Environment Variables in PowerShell? - SharePoint Diary
October 7, 2025 - To manage environment variables from PowerShell, You can use the [System.Environment] methods from .NET class that sets the environment variables permanently or by using the $env: in PSDrive for temporary session variables.
🌐
Configu
configu.com › home › setting environment variables in powershell: a practical guide
Setting Environment Variables in PowerShell: A Practical Guide - Configu
January 17, 2025 - This method is temporary; the modification will reset once the PowerShell session ends: # Temporarily add a new directory to the PATH environment variable $env:PATH = $env:PATH + ';C:\bin' For a more lasting modification—such as updating the PATH environment variable across all sessions—you can use the .NET method with the Machine target. This command permanently updates the system’s environment settings by adding C:\bin to the PATH variable in the machine’s registry:
🌐
ShellGeek
shellgeek.com › home › powershell › set environment variable using powershell
Set Environment Variable using PowerShell - ShellGeek
April 14, 2024 - Use the .Net framework library [System.Environment] method in PowerShell to set an environment variable permanently for the user and machine for permanent use.
🌐
PowerShell Test-Path
powershellfaqs.com › set-and-get-environment-variables-in-powershell
How to Set and Get Environment Variables in PowerShell?
September 4, 2024 - Once the session is closed, these variables are discarded. To set a temporary environment variable, use the $env: prefix followed by the variable name and the assignment operator (=). Here’s an example: ... This command sets an environment variable named MyTempVar with the value TemporaryValue.
🌐
GitHub
github.com › PowerShell › PowerShell › issues › 26725
[Environment]::SetEnvironmentVariable() fixes and improvements · Issue #26725 · PowerShell/PowerShell
January 26, 2026 - Summary of the new feature / enhancement [Environment]::SetEnvironmentVariable("Variable", $null) should delete the environmentvariable and when passing [Environment]::SetEnvironmentVariable("Variable", "") it keeps the current behaviour...
Author   AkitoTheGambler