Variables

Enumerate

Get-Variable

Displays current variables

gci variable:

Same as above

Get-ChildItem env:

List all Environment Variables

set

Lists all environment variables in the Command Prompt.

echo %VARIABLE_NAME%

Shows the value of a specific environment variable in the Command Prompt.

$env:VARIABLE_NAME

Retrieves the value of a specific environment variable in PowerShell.

printenv

Windows Subsystem for Linux (WSL) environment.

Modify

Get-Variable

Names are displayed without the preceding <$>

Clear-Variable -Name MyVariable

Delete the value of a Variable

Remove-Variable -Name MyVariable

Delete the Variable

$MyVariable = 1, 2, 3

Creates the MyVariable with 1,2,3

$Processes = Get-Process

Creates a Variable with the results of Get-Process

$Today = (Get-Date).DateTime

Creates a combined Date/Time variable from the results of Get-Date

Set-Item -Path Env:/A -Value 1

Sets environment variable 'A' to '1'

Automatic Variables

$$

Last token in the last line received by the session.

$?

Execution status of the last operation.

$^

First token in the last line received by the session.

$_

Contains the current object in the pipeline object.

$ARGS

Array of the undeclared parameters and/or parameter values passed to a function, script, or script block.

$ERROR

An array of error objects that represent the most recent errors.

$FALSE

Represent FALSE in commands and scripts instead of using the string "false".

$FOREACH

Enumerator (not the resulting values) of a ForEach loop.

$HOME

Full path of the user’s home directory.

$LASTEXITCODE

Exit code of the last Windows-based program that was run.

$MATCHES

Works with the -match and -notmatch operators.

$NULL

Automatic variable that contains a NULL or empty value.

$PID

Process identifier (PID) of the process hosting the current PowerShell session.

$PROFILE

Full path of the PowerShell profile for the current user and the current host application.

$PSVERSIONTABLE

Read-only hash table displaying details about the version of PowerShell running in the current session.

$TRUE

Used to represent TRUE in commands and scripts.

Registry Locations

Environment variables in Windows are stored in the registry in two primary locations:

System Variables:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

This key contains environment variables that are set system-wide and affect all users on the system.

User Variables:

HKEY_CURRENT_USER\Environment

This key stores environment variables that are specific to the currently logged-in user.

When you set or modify environment variables via system settings or using tools like setx, the changes are reflected in these registry locations.

Last updated

Was this helpful?