PowerShell exposes environmental variables via the $Env: scope (you can read more about scopes here)

So to access the USERPROFILE environmental variable, you could do the following (note, I'm using Write-Host in place of echo here, see this answer for details on the difference between the two):

Write-Host $Env:USERPROFILE

PowerShell also exposes a number of automatic variables and these are made available to all scripts and commands. $HOME for example is one such automatic variable.

For further information on these, see Automatic Variables

Answer from NiMux on Stack Overflow
🌐
Netwrix
netwrix.com › home › resources › blog › powershell environment variables
PowerShell Environment Variables
August 25, 2025 - PowerShell environment variables store system and user configuration as key-value pairs accessible across processes and scripts. They can be listed with Get-ChildItem Env:, accessed with $Env:<Name>, modified with Set-Item or [Environment]:...
Discussions

Please help me understand the purpose of environments in PowerShell
Powershell has no "Environments". If they " look similar to variables", you're most likely talking about environment variables. They are not related to Powershell, they are part of the environment the OS provides to process. They provide information to the processes. Partly they are maintained by the OS itself, partly they are user maintained to configure and control processes. More on reddit.com
🌐 r/PowerShell
42
11
December 25, 2023
How can I get and change environment variables' value in Windows Powershell
Discussed in #9473 Originally posted by xiaolinggnb August 16, 2024 I'm using Windows 11 and willing to get the environment variable value of gh and change them in Windows Powershell, which are lis... More on github.com
🌐 github.com
1
1
powershell - permanent environment variables
I’m trying to append some environment variables to Path and found this: $path = $env:Path + ";valuehere" [environment]::SetEnvironmentVariable('Path',$path) and when I do $env:Path, it shows, but not when I open up the gui for environement variables and when I restart, powershell no longer ... More on community.spiceworks.com
🌐 community.spiceworks.com
8
6
February 24, 2023
Using Environment Variables in PowerShell Scripts
hey guys, how's everyone doing? I'm exploring possibilities with scripts in atera and would like recommendations for working with environment variables. More on community.atera.com
🌐 community.atera.com
October 25, 2024
People also ask

How to check if an environment variable exists in PowerShell?

To check if a specific environment variable exists, you can use the following command.

Test-Path Env:MY_VAR Replace MY_VAR with the name of the environment variable you want to check. If the variable exists, it will return True; if not, it will return False.

🌐
netwrix.com
netwrix.com › home › resources › blog › powershell environment variables
PowerShell Environment Variables
How do I list all variables in PowerShell?

To list all variables in your PowerShell session, you can use:

Get-ChildItem Env:

This command will show all currently defined variables in the session, including environment variables.

🌐
netwrix.com
netwrix.com › home › resources › blog › powershell environment variables
PowerShell Environment Variables
🌐
Codecademy
codecademy.com › docs › powershell › environment variables
PowerShell | Environment Variables | Codecademy
May 16, 2023 - Environment Variables are name-value pairs that store information related to the current environment. These global variables can be accessed across commands and programs.
🌐
Reddit
reddit.com › r/powershell › please help me understand the purpose of environments in powershell
r/PowerShell on Reddit: Please help me understand the purpose of environments in PowerShell
December 25, 2023 -

Hi all!,

I am a beginner in PowerShell and trying to learn to advance myself outside of my desktop support job. I took a coding video course on Udemy and code academy and I am not trying to do solo with the help of chatgpt for guidance while looking at another udemy course strictly catered to AD use of PowerShell. While I was looking at tutorial videos, these videos have mentions briefly about the purpose of environments and I just dont really understand there use case since they look similar to variables. I tried using catgut to help me understand but I'm still getting confused about its purpose and use case. Are they similar to environments like in docker(brief learned about it)?

Any tutorial videos that explain about this would be greatly appreciated!

Find elsewhere
🌐
Opensource.com
opensource.com › article › 19 › 9 › environment-variables-powershell
Environment variables in PowerShell | Opensource.com
With environment variables, you ... PowerShell, environment variables are stored in the Env: "drive", accessible through the PowerShell environment provider, a subsystem of PowerShell....
🌐
GitHub
github.com › cli › cli › discussions › 9475
How can I get and change environment variables' value in Windows Powershell · cli/cli · Discussion #9475
If you'd like the variable to be more permanent, you should add it to your .bashrc, .bash_profile, or similar so it gets exported every time you start a new shell session. Here's some Microsoft documentation about managing environment variables on Windows, if that's the environment you are using.
Author   cli
🌐
Atera's IT Community
community.atera.com › news and announcements
Using Environment Variables in PowerShell Scripts — Atera's IT Community
October 25, 2024 - I'm exploring possibilities with scripts in atera and would like recommendations for working with environment variables. In my tests I noticed that the script submission occurs through a powershell session with different values than the real user.
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › powertip: use windows powershell to display all environment variables
PowerTip: Use Windows PowerShell to display all Environment variables - Scripting Blog [archived]
January 15, 2020 - When PowerShell is locked down in safe mode, you are not allowed to call methods on object for security reasons, so using System.Environment is out of the question then. The right way to get to environment variables in PowerShell is the Env: PSDrive.
🌐
Configu
configu.com › home › setting environment variables in powershell: a practical guide
Setting Environment Variables in PowerShell: A Practical Guide - Configu
January 17, 2025 - In PowerShell, environment variables can be manipulated to retrieve system information, such as the current working directory or operating system version, and are crucial for automating tasks across different environments.
🌐
IT Pro Today
itprotoday.com › powershell › introduction-to-powershell-environment-variables
Introduction To PowerShell Environment Variables
Today's distributed computing environments require a cloud strategy that goes well beyond choosing the best security tools.
🌐
Itenium
itenium.be › home › blog › managing environment variables with powershell
Managing Environment Variables with PowerShell - Itenium
August 24, 2018 - # Managing current session environment ... [Environment]::SetEnvironmentVariable("yaye", "value", "Machine") When using the PowerShell $env:key = "val", the environment changes are available only for the current session....
Top answer
1 of 6
3
MTonn:

Thanks, Managed to get something to work… Will do some more testing but it appears to be working correctly now.

#This section are the variables for each of the environmental variables.
        $oracle_envar = ';C:\oracle'
        $odac12_envar = ';C:\oracle\odac12'
        $odac12bin_envar = ';C:\oracle\odac12\bin'
        $odac12_32bit_envar = ';C:\oracle\odac12_32bit'
        $odac12bin_32bit_envar = ';C:\oracle\odac12_32bit\bin'

        $env = Get-ChildItem Env:\Path

        if ($env -contains '*C:\oracle*')
        {
        
        }
        elseif ([System.Environment]::Is64BitProcess) 
        {
        [System.Environment]::SetEnvironmentVariable('PATH', $Env:Path + "$odac12_envar $odac12bin_envar $odac12_32bit_envar $oracle_envar $odac12bin_32bit_envar", 'Machine')
        }
        else
        {
        [System.Environment]::SetEnvironmentVariable('PATH', $Env:Path + "$odac12_32bit_envar $oracle_envar $odac12bin_32bit_envar", 'Machine')
        } 

And just because this will bug me, you can reduce the lines of code necessary with something like this:

#This section are the variables for each of the environmental variables.
        $oracle_envar = ';C:\oracle'
        $odac12_envar = ';C:\oracle\odac12'
        $odac12bin_envar = ';C:\oracle\odac12\bin'
        $odac12_32bit_envar = ';C:\oracle\odac12_32bit'
        $odac12bin_32bit_envar = ';C:\oracle\odac12_32bit\bin'

        $env = Get-ChildItem Env:\Path

        if ([System.Environment]::Is64BitProcess -and $env -notcontains '*C:\oracle*')
        {
			[System.Environment]::SetEnvironmentVariable('PATH', $Env:Path + "$odac12_envar $odac12bin_envar $odac12_32bit_envar $oracle_envar $odac12bin_32bit_envar", 'Machine')
        }
        elseif (-not [System.Environment]::Is64BitProcess -and $env -notcontains '*C:\oracle*')
        {
			[System.Environment]::SetEnvironmentVariable('PATH', $Env:Path + "$odac12_32bit_envar $oracle_envar $odac12bin_32bit_envar", 'Machine')
        }
		else
		{
			Write-Output "This machine already has the environment variable set."
		}

But then when I got done it ended up being more lines of code because I added an elseif to better determine if the 32-bit version is already set or not.

2 of 6
4

Could someone help me out… new to PowerShell and looking to create the following values in the machine environmental path for part of an installation.

This works as expected… But the if the script is ran for a second time it will add these values again. I need to be able to check if they exist and if they do skip and if they don’t create them.

Any help would be great.

        #This section are the variables for each of the environmental variables.
        $oracle_envar = ';C:\oracle'
        $odac12_envar = ';C:\oracle\odac12'
        $odac12bin_envar = ';C:\oracle\odac12\bin'
        $odac12_32bit_envar = ';C:\oracle\odac12_32bit'
        $odac12bin_32bit_envar = ';C:\oracle\odac12_32bit\bin'

        #Checks if the system is 32bit or 64bit and sets the Environmental Variable path to contain the required locations.
        if ([System.Environment]::Is64BitProcess) 
        {
        [System.Environment]::SetEnvironmentVariable('PATH', $Env:Path + "$odac12_envar $odac12bin_envar $odac12_32bit_envar $oracle_envar $odac12bin_32bit_envar", 'Machine')
        }
        else 
        {
        [System.Environment]::SetEnvironmentVariable('PATH', $Env:Path + "$odac12_32bit_envar $oracle_envar $odac12bin_32bit_envar", 'Machine')
}

If someone has a better way of completing this with powershell please chime in.

🌐
GitHub
gist.github.com › 4482851
Environment variable Powershell · GitHub
Environment variable Powershell. GitHub Gist: instantly share code, notes, and snippets.
🌐
Leyaa
leyaa.ai › codefly › learn › powershell › part-3 › powershell-environment-variables › try
Environment variables in PowerShell - Interactive Code Practice | Leyaa.ai
Using incorrect variable names like PATH instead of MY_VAR. Not using quotes around string values. ... To create or update an environment variable, assign a value to $Env:VariableName.
🌐
Computer Performance
computerperformance.co.uk › home › powershell
PowerShell Basics: $Env: - Environment Variables | Examples & Scripts
January 16, 2019 - Once you know that Env is a drive, then you can list its variables and their values. This is just like you would use PowerShell to list the folders and files in the C: drive. # List PowerShell's Environmental Variables Get-Childitem -Path Env:* | Sort-Object Name