---
title: "PowerShell Environment Variables: A Deep Dive"
description: "Learn about Windows environment variables and how to manipulate PowerShell environment variables effectively."
canonical: "https://adamtheautomator.com/powershell-environment-variables/"
---

# PowerShell Environment Variables: A Deep Dive

> Learn about Windows environment variables and how to manipulate PowerShell environment variables effectively.

Source: https://adamtheautomator.com/powershell-environment-variables/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![PowerShell Environment Variables: A Deep Dive](https://adamtheautomator.com/wp-content/uploads/2019/09/powershell-set-environment-variable.png)

# PowerShell Environment Variables: A Deep Dive

[![](https://secure.gravatar.com/avatar/995fcc2faf7fc9d3f15061d8e6c5b84f4b751d96502ef7afb4f9f648a58b36a4?s=192&d=mm&r=g)Stuart Squibb](https://adamtheautomator.com/author/stuart/)12 September 201913 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Assumptions](#assumptions)
*   [What are Environment Variables?](#what-are-environment-variables)
*   [Common Environment Variables](#common-environment-variables)
*   [Environment Variable Scopes](#environment-variable-scopes)
*   [Environment Variable Scope “Hierarchy”](#environment-variable-scope-hierarchy)
*   [Environment Variable Scope Types](#environment-variable-scope-types)
*   [Machine](#machine)
*   [User](#user)
*   [Process](#process)
*   [Environment Variables in the Registry](#environment-variables-in-the-registry)
*   [Don’t Use the Registry to Manage Environment Variables](#don-t-use-the-registry-to-manage-environment-variables)
*   [Environment Variable Registry Locations and Querying](#environment-variable-registry-locations-and-querying)
*   [View and Set Windows Environment Variables via the GUI](#view-and-set-windows-environment-variables-via-the-gui)
*   [The Env: PSDrive and Provider](#the-env-psdrive-and-provider)
*   [Switching to the Env: Drive](#switching-to-the-env-drive)
*   [Tab-Completion with the Env: Drive](#tab-completion-with-the-env-drive)
*   [Listing Environment Variables with Env:](#listing-environment-variables-with-env-)
*   [Listing Environment Variables Using a Wildcard with Env:](#listing-environment-variables-using-a-wildcard-with-env-)
*   [Finding Environment Variable Values with Env:](#finding-environment-variable-values-with-env-)
*   [Demo: Inserting Environment Values in a String](#demo-inserting-environment-values-in-a-string)
*   [Setting an Environment Variable (And Creating) with Env:](#setting-an-environment-variable-and-creating-with-env-)
*   [Copying an Environment Variable with Env:](#copying-an-environment-variable-with-env-)
*   [Removing an Environment Variable with Env:](#removing-an-environment-variable-with-env-)
*   [Renaming an Environment Variable with Env:](#renaming-an-environment-variable-with-env-)
*   [$Env: Variables](#-env-variables)
*   [Getting an Environment Variable with $Env:](#getting-an-environment-variable-with-env-)
*   [Setting or Creating an Environment Variable with $Env:](#setting-or-creating-an-environment-variable-with-env-)
*   [Removing an Environment Variable with $Env:](#removing-an-environment-variable-with-env--1)
*   [Using the \[System.Environment\] .NET Class](#using-the-system-environment-net-class)
*   [Listing Environment Variables with \[System.Environment\]](#listing-environment-variables-with-system-environment-)
*   [Getting Single Environment Variables with \[System.Environment\]](#getting-single-environment-variables-with-system-environment-)
*   [GetEnvironmentVariables()](#getenvironmentvariables-)
*   [GetEnvironmentVariable()](#getenvironmentvariable-)
*   [Setting an Environment Variable with \[System.Environment\]](#setting-an-environment-variable-with-system-environment-)
*   [Removing an Environment Variable with \[System.Environment\]](#removing-an-environment-variable-with-system-environment-)
*   [Useful PowerShell Environment Variables](#useful-powershell-environment-variables)
*   [PSExecutionPolicyPreference](#psexecutionpolicypreference)
*   [PSModulePath](#psmodulepath)
*   [Summary](#summary)
*   [Further Reading](#further-reading)

Using PowerShell to set Windows environment variables, read environment variables and create new environment variables is easy once you know the trick. The tricks you learn in this article will work for Windows 10 environment variables as well as any Windows client/server after Windows 7 SP1/Windows Server 2008.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

PowerShell provides many different ways to interact with Windows environment variables from the `$env:` PSDrive. the registry, and the `[System.Environment]` .NET class. You’ll learn about each method including understand environment variable scope in this step-by-step walkthrough.

## Assumptions

Throughout this article, I’ll be using Windows PowerShell 5.1 on Windows 10. But if you have any version of Windows PowerShell later than v3 on Windows 7 SP1 or later, the techniques I’m about to show you should work just fine.

## What are Environment Variables?

Environment variables, as the name suggests, store information about the environment that is used by Windows and applications. Environment variables can be accessed by graphical applications such as Windows Explorer and plain text editors like Notepad, as well as the cmd.exe and PowerShell.

Using environment variables helps you to avoid hard-coding file paths, user or computer names and much more in your PowerShell scripts or modules.

## Common Environment Variables

As you begin to learn more about how to work with environment variables in PowerShell, you’ll come across many different variables. Some are more useful than others. Below is a list of some of the common environment variables and their usage for reference.

| Variable | Usage |
| --- | --- |
| ClientName | The name of the remote computer connected via a Remote Desktop session. |
| SessionName | This helps to identify if the current Windows session is regarded by the operating system as running at the console. For console sessions SessionName will be ‘Console’. Enhanced Session connections to Hyper-V Virtual Machines do not report SessionName as ‘Console’, whereas Standard Sessions do. |
| ComputerName | The name of the computer. |
| SystemRoot and Windir | The path to the current Windows installation. |
| ProgramFiles and ProgramFiles(x86) | The default locations for x64 and x86 programs. |
| ProgramW6432 | The default location for programs, avoiding 32/64 bit redirection. This variable only applies for 32 bit processes running on a 64 bit platform. This means that you can use it to identify when a 32 bit instance of PowerShell is running on a 64 bit system. |
| UserDNSDomain | The Fully Qualified Domain Name of the Active Directory domain that the current user logged on to. Only present for domain logons. |
| UserDomain | The NETBIOS-style name of the domain that the current user logged on to. Can be a computer name if there’s no domain. |
| UserDomainRoamingProfile | The location of the central copy of the roaming profile for the user, if any. Only present for domain logons. |
| UserName | The name of the currently logged on user. |
| UserProfile | The location of the profile of the current user on the local computer. |

## Environment Variable Scopes

There are three _scopes_ of environment variables. Think of scopes as layers of variables that build up to give a total picture. Combined, these “layers” provide many different environment variables to any running process in Windows.

### Environment Variable Scope “Hierarchy”

Each of these “layers” either combine or overwrite one another. They are defined in a hierarchy like: _machine_ –> _user_ –> _process_ with each scoped variable overwriting the parent variable if one exists in the parent scope.

For example, a common environment variable is `TEMP`. This variable stores the folder path to Windows’ local temporary folder. This environment variable is set to:

*   `C:\WINDOWS\TEMP` in the _machine_ scope
*   `C:\Users\<username>\AppData\Local\Temp` in the _user_ scope
*   `C:\Users\<username>\AppData\Local\Temp` in the _process_ scope.

If there’s no `TEMP` environment variable set in the _user_ scope then the end result will be `C:\WINDOWS\TEMP`.

### Environment Variable Scope Types

There are three different environment variable scopes in Windows.

### Machine

Environment variables in the _machine_ scope are associated with the running instance of Windows. Any user account can read these, but setting, changing or deleting them needs to done with elevated privileges.

### User

Environment variables in the _user_ scope are associated with the user running the current process. _User_ variables overwrite _machine-_scoped variables having the same name.

> _Note: Microsoft recommends that Machine and User scoped environment variable values contain no more than 2048 characters._

### Process

Environment variables in the _process_ scope are a combination of the _machine_ and _user_ scopes, along with some variables that Windows creates dynamically.

Below is a list of environment variables available to a running process. All of these variables are dynamically created.

*   `ALLUSERSPROFILE`
*   `APPDATA`
*   `COMPUTERNAME`
*   `HOMEDRIVE`
*   `HOMEPATH`
*   `LOCALAPPDATA`
*   `LOGONSERVER`
*   `PROMPT`
*   `PUBLIC`
*   `SESSION`
*   `SystemDrive`
*   `SystemRoot`
*   `USERDNSDOMAIN`
*   `USERDOMAIN`
*   `USERDOMAIN_ROAMINGPROFILE`
*   `USERNAME`
*   `USERPROFILE`

## Environment Variables in the Registry

Environment variables are stored in two registry locations, one for the user scope and one for the machine scope.

Related:[How to use PowerShell to Get a Registry Value (PS Drives and .NET)](https://adamtheautomator.com/powershell-to-get-a-registry-value/)

### Don’t Use the Registry to Manage Environment Variables

There’s a catch when making changes to variables inside of the registry. Any running processes will not see variable changes in the registry. Processes only see the registry variables and values that were present when the process was started, unless Windows notifies them that there has been a change.

Instead of modifying the registry directly, you can use a .NET class instead. The .NET `[System.Environment]` class can modify machine and user_–_scoped environment variables and handle the registry housekeeping for you.

Modifying environment variables in the registry directly, whilst possible, doesn’t make sense. The .NET class offers a simpler, Microsoft-supported approach. You’ll learn about using the `[System.Environment]` .NET class later in this article.

### Environment Variable Registry Locations and Querying

I hope you’ve been convinced to not modify the registry directly but if you’d like to take a peek at what’s in there, you can find all user environment variables in the `HKEY_CURRENT_USER\Environment` key. Machine-scoped environment variables are stored at `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment`.

Inside of either of these keys lies registry values of type `REG_SZ` or `REG_EXPAND_SZ`. `REG_EXPAND_SZ` values hold environment variables embedded as part of their value. These environment variables are expanded when the value is retrieved.

To demonstrate this, use the `REG` utility. This is a small command-line utility included with Windows.

Query the `TEMP` environment variable as seen below. Run `REG` with the `QUERY` parameter to retrieve the value of the `TEMP` variable.

```bash
> REG QUERY HKCU\Environment /V TEMP

HKEY_CURRENT_USER\Environment     
    TEMP    REG_EXPAND_SZ    %USERPROFILE%\AppData\Local\Temp
```

> _You’ll sometimes notice environment variables displayed surrounded by percentage symbols (`%COMPUTERNAME%`) like above. This is the old-school way of showing environment variables via cmd.exe and batch files. Know that PowerShell does not recognize this format._

The `REG` utility allows us to see the native value of the registry value. The value type is `REG_EXPAND_SZ` and the value contains the `%USERPROFILE%` environment variable.

If you’d rather use PowerShell to retrieve the registry value, you can so using the `Get-Item` cmdlet as shown below.

```powershell
PS51> Get-ItemProperty -Path HKCU:\Environment -Name TEMP

TEMP         : C:\Users\<your username>\AppData\Local\Temp
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Environment
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER
PSChildName  : Environment
PSDrive      : HKCU
PSProvider   : Microsoft.PowerShell.Core\Registry
```

## View and Set Windows Environment Variables via the GUI

To see a GUI view of the user and system environment variables, run _SystemPropertiesAdvanced.exe_ from PowerShell, a command prompt or from Windows Key+R to display the _System Properties Advanced_ tab. Click on the _EnvironmentVariables_ button, which is highlighted in the image below.

![The System Properties dialog, Advanced tab](/wp-content/uploads/2019/09/SystemPropertiesAdvanced.png)

_The System Properties dialog, Advanced tab_

The Environment Variables dialog, shown below, allows you to view, create and modify user and machine-scoped environment variables. Note that the dialog refers to machine scoped variables as _System variables_.

![The Environment Variables dialog](/wp-content/uploads/2019/09/EnvironmentVariables.png)

_The Environment Variables dialog_

Now that you have an understanding of environment variables, let’s get to what you’re here for, [managing them with PowerShell](https://adamtheautomator.com/powershell-scheduled-task/)!

There are a few different ways you can interact with environment variables using PowerShell.

*   **The `Env:` PSDrive and Provider** – session-based. Only sets the values of environment variables for the current PowerShell session
*   **`$env:` variables** – session-based. Only sets the values of environment variables for the current PowerShell session
*   **The `[System.Environment]` .NET Class** – allows you to persist user and system-scoped environment variables across sessions and reboots

### The `Env:` PSDrive and Provider

One of the best way to read environment variables is a PowerShell concept known as PowerShell drives (PS drives). A PS drive allows you to treat environment variables as if they are a file system through the `Env:` drive.

#### Switching to the `Env:` Drive

Like all PS drives, you reference it via paths like `Env:\TEMP`, `Env:\COMPUTERNAME`, etc. But to save some keystrokes, switch to the `Env:` drive just as you would any file system drive, as shown below.

```powershell
PS51> cd Env:
PS51 Env:\>

## or

PS51> Set-Location Env:
PS Env:\>
```

#### Tab-Completion with the `Env:` Drive

You can use the same commands you would use to access a file system, such as `Get-Item` and [`Get-ChildItem`](https://adamtheautomator.com/get-childitem/ "Get-ChildItem") to access environment variables. But instead of the file system, you’re reading the `Env:` drive.

Because environment variables are stored in a PS drive, you can use the tab completion feature of PowerShell to cycle through the available variables, as shown below.

![Env: tab completion - finding environment variables starting with the letter C](https://adamtheautomator.com/wp-content/uploads/2020/06/TabCompletion-1-1.gif)

_Env: tab completion – finding environment variables starting with the letter C_

Let’s now jump into a couple of examples of how you can use the `Env:` PS drive to work with environment variables.

### Listing Environment Variables with `Env:`

Related:[Understanding and Building New PS Drives in PowerShell](https://adamtheautomator.com/new-psdrive/)

```powershell
PS51> Get-Item -Path Env:
PS51> Get-Item -Path Env:USERNAME
```

### Listing Environment Variables Using a Wildcard with `Env:`

```powershell
PS51> Get-Item -Path Env:user*
```

### Finding Environment Variable Values with `Env:`

The results of these commands are key/value `[System.Collections.DictionaryEntry]` .NET objects. These objects hold the environment variable’s name in the `Name` property and the value in the `Value` property.

You can access a specific value of an environment variable by wrapping the `Get-Item` command reference in parentheses and referencing the `Value` property as shown below:

```powershell
PS51> (Get-Item -Path Env:computername).Value
MYCOMPUTERHOSTNAME
```

In situations where you need to only return certain environment variables, use standard PowerShell cmdlets such as `Select-Object` and [`Where-Object`](https://adamtheautomator.com/powershell-where-object/ "Where-Object") to select and filter the objects returned by the `Env:` provider.

In the example below, only the environment variable `COMPUTERNAME` is returned.

```powershell
PS51> Get-ChildItem -Path Env: | Where-Object -Property Name -eq 'COMPUTERNAME'
```

As an alternative method, use the [`Get-Content`](https://adamtheautomator.com/powershell-get-content/ "Get-Content") cmdlet. This cmdlet returns a `[String]` object containing the value of the environment variable. This object is simpler to deal with as it returns only the value, rather than an object with `Name` and `Value` properties.

```powershell
PS51> Get-Content -Path Env:\COMPUTERNAME
```

#### Demo: Inserting Environment Values in a String

Using `Get-Content`, you can find the value of an environment variable and insert the `COMPUTERNAME` environment variable, for example, into a text string.

```powershell
PS51> 'Computer Name is: {0}' -f (Get-Content -Path Env:COMPUTERNAME)
Computer Name is: MYCOMPUTER
```

### Setting an Environment Variable (And Creating) with `Env:`

Create new environment variables with PowerShell using the `New-Item` cmdlet. Provide the name of the environment variable in the form `Env:\<EnvVarName>` for the `Name` value and the value of the environment variable for the `Value` parameter as shown below.

```powershell
PS51> New-Item -Path Env:\MYCOMPUTER -Value MY-WIN10-PC
Name                           Value
----                           -----
MYCOMPUTER                     MY-WIN10-PC
```

Use the `Set-item` cmdlet to set an environment variable, or create a new one if it doesn’t already exist. You can see below using the `Set-Item` cmdlet, you can both create or modify and environment variable.

```powershell
PS51> Set-Item -Path Env:testvariable -Value "Alpha"
```

### Copying an Environment Variable with `Env:`

Sometimes the situation arises that you need to replicate the value of an environment variable. You can do this using the [`Copy-Item`](https://adamtheautomator.com/copy-item/ "Copy-Item") cmdlet.

Below you can see that the value of the `COMPUTERNAME` variable is copied to `MYCOMPUTER`, overwriting its existing value.

```powershell
PS51> Get-Item -Path Env:\MYCOMPUTER

Name                           Value
----                           -----
MYCOMPUTER                     MY-WIN10-PC

PS51> Copy-Item -Path Env:\COMPUTERNAME -Destination Env:\MYCOMPUTER
PS51> Get-Item -Path Env:\MYCOMPUTER

Name                           Value
----                           -----
MYCOMPUTER                     WIN10-1903
```

### Removing an Environment Variable with `Env:`

Situations will arise where an environment variable is no longer needed. You can remove environment variables using one of three methods:

*   Use the `Set-Item` cmdlet to set an environment variable to an empty value

```powershell
PS51> Set-Item -Path Env:\MYCOMPUTER -Value ''
```

*   Use the [`Remove-Item`](https://adamtheautomator.com/powershell-to-delete-files/ "Remove-Item") cmdlet.

```powershell
PS51> Remove-Item -Path Env:\MYCOMPUTER
```

*   Use the `Clear-Item` cmdlet.

```powershell
PS51> Clear-Item -Path Env:\MYCOMPUTER
```

### Renaming an Environment Variable with `Env:`

In situations where the name of an environment variable needs to be changed, you have the option to rename, rather than delete and recreate with the `Env:` provider.

Use the `Rename-Item` cmdlet to change the name of an environment variable whilst keeping its value. Below you can see that you can see that the `MYCOMPUTER` variable is renamed to `OLDCOMPUTER` whilst retaining its value.

```powershell
PS51> Rename-Item -Path Env:\MYCOMPUTER -NewName OLDCOMPUTER
PS51> Get-Item -Path OLDCOMPUTER
Name                           Value
----                           -----
OLDCOMPUTER                    WIN10-1903
```

## `$Env:` Variables

Having mastered the `Env:` drive to treat environment variables as files, this section shows you how to treat them as variables. Another way you can manage in-session environment variables is using the the [PowerShell Expression Parser](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-5.1). This feature allows you to use the `$Env:` scope to access environment variables.

### Getting an Environment Variable with `$Env:`

Using the `$Env` scope, you can reference environment variables directly without using a command like `Get-Item` as shown below.

```powershell
PS51> $env:computername
```

This method makes it easy to insert environment variables into strings like below:

```powershell
PS51> "The Computer Name is {0}" -f $env:computername
The Computer Name is WIN10-1809
PS51> "The Computer Name is $env:computername"
The Computer Name is WIN10-1809
```

### Setting or Creating an Environment Variable with `$Env:`

Setting an environment variable using this method is straightforward. This will also create a new environment variable if one does not already exist like below.

```powershell
PS51> $env:testvariable = "Alpha"
```

Use the `+=` syntax to add to an existing value, rather than overwriting it.

```powershell
PS51> $env:testvariable = "Alpha"
PS51> $env:testvariable += ",Beta"

PS51> $env:testvariable
Alpha,Beta
```

### Removing an Environment Variable with `$Env:`

To remove an environment variable using this method, simple set its value to an empty string.

```powershell
PS51> $env:testvariable = ''
```

## Using the `[System.Environment]` .NET Class

The .NET class `[System.Environment]` offers methods for getting and setting environment variables also. This is only method to access various environment scopes directly and set environment variables that survive across PowerShell sessions.

> _In all of the following examples, if no scope is provided, the process scope is assumed._

When using the `[System.Environment]`, you’ll use a few different [.NET static class](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members) methods. You don’t need to understand what a static method is. You only need to understand to use any of the techniques you’re about to learn, you’ll need to first reference the class (`[System.Environment]`) followed by two colons (`::`) then followed by the method.

### Listing Environment Variables with `[System.Environment]`

If you’d like to see all environment variables in a particular scope, you’d use the `GetEnvironmentVariables` method. This method returns all environment variables by the scope specified as the method argument (in parentheses).

```powershell
PS51> [System.Environment]::GetEnvironmentVariables('User')
PS51> [System.Environment]::GetEnvironmentVariables('Machine')
PS51> [System.Environment]::GetEnvironmentVariables('Process')

# The same as Process
PS51> [System.Environment]::GetEnvironmentVariables()
```

### Getting Single Environment Variables with `[System.Environment]`

If you need to find a specific environment variable you can do so two different ways.

*   `GetEnvironmentVariables().<var name>` – not recommended
*   `GetEnvironmentVariable('<var name>','<scope>')`

#### `GetEnvironmentVariables()`

Using the `GetEnvironmentVariables()` method, you use dot notation to reference the value. Enclose the `[System.Environment]` class and static method reference in parentheses followed by a dot then the name of the environment variable like below:

```powershell
PS51> ([System.Environment]::GetEnvironmentVariables()).APPDATA
```

> _Note that when referencing environment variables this way, you must ensure you match capitalization! In the example above, try to reference the `APPDATA` variable using `appdata`. Use the `GetEnvironmentVariable()` instead._

#### `GetEnvironmentVariable()`

Rather than using the `GetEnvironmentVariables()` method, instead use `GetEnvironmentVariable()` to find single environment variables. It gets around the issue with capitalization and also allows you to specify the scope.

To use this method, specify the environment variable name and the scope you’d like to look for that variable in separated by comma.

```powershell
PS51> [System.Environment]::GetEnvironmentVariable('ComputerName','User')
# Blank

PS51> [System.Environment]::GetEnvironmentVariable('ComputerName','Machine')
# Blank

PS51> [System.Environment]::GetEnvironmentVariable('ComputerName','Process')  WIN10-1903

# The same as Process
PS51> [System.Environment]::GetEnvironmentVariable('ComputerName')
WIN10-1903
```

### Setting an Environment Variable with `[System.Environment]`

Use the `SetEnvironmentVariable()` method to set the value of an environment variable for the given scope, or create a new one if it does not already exist.

When setting variables in the process scope, you’ll find that the process scope is volatile while changes to the user and machine scopes are permanent.

```powershell
PS51> [System.Environment]::SetEnvironmentVariable('TestVariable','Alpha','User')

PS51> [System.Environment]::SetEnvironmentVariable('TestVariable','Alpha','Process')

PS51> [System.Environment]::SetEnvironmentVariable('TestVariable','Alpha','Machine')

 # The same as Process
PS51> [System.Environment]::SetEnvironmentVariable('TestVariable','Alpha')
```

> Note: Calling the SetEnvironmentVariable method with a variable name or value of 32767 characters or more will cause an exception to be thrown.

### Removing an Environment Variable with `[System.Environment]`

Use the `SetEnvironmentVariable()` method to remove an environment variable for the given scope by setting its value to an empty string.

```powershell
PS51> [System.Environment]::SetEnvironmentVariable('TestVariable', '', 'User')

PS51> [System.Environment]::SetEnvironmentVariable('TestVariable', '', 'Process')

PS51> [System.Environment]::SetEnvironmentVariable('TestVariable', '', 'Machine')

# The same as process
PS51> [System.Environment]::SetEnvironmentVariable('TestVariable', '')
```

## Useful PowerShell Environment Variables

Like many other Windows applications, PowerShell has some environment variables of it’s own. Two useful environment variables to know about are `PSExecutionPolicyPreference` and `PSModulePath`.

### `PSExecutionPolicyPreference`

The `PSExecutionPolicyPreference` environment variable stores the current PowerShell execution policy. It is created if a session-specific PowerShell execution policy is set by:

*   Running the `Set-ExecutionPolicy` cmdlet with a `Scope` parameter of `Process`
*   Running the `powershell.exe` executable to start a new session, using the `ExecutionPolicy` command line parameter to set a policy for the session.

### `PSModulePath`

The `PSModulePath` environment variable contains the path that PowerShell searches for modules if you do not specify a full path. It is formed much like the standard `PATH` environment variable, with individual directory paths separated by a semicolon.

```powershell
PS51> $env:PSModulePath
C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
```

> _Quick tip: Split each folder to get a string array to process each path individually using $env:PSModulePath.split(‘;’)_

## Summary

Environment variables are a useful method for getting information about a running system, or storing information across sessions and reboots. Whether you’re just reading the default Windows operating system environment variables and creating your own, you now can manage them using a variety of ways with PowerShell!

## Further Reading

*   **_[about\_Environment\_Variables](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.2)_**
*   **_The .NET [\[System.Environment\] Class](https://docs.microsoft.com/en-us/dotnet/api/system.environment?view=netframework-4.0) on Microsoft docs_**
*   **_[Learn the PowerShell string format and expanding strings](https://adamtheautomator.com/powershell-string-format/)_**

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-environment-variables%2F&text=PowerShell%20Environment%20Variables%3A%20A%20Deep%20Dive)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-environment-variables%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-environment-variables%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
