Sleep or Pause to PowerShell Script

Last Updated on August 7, 2025 by Arnav Sharma

Setting and managing environment variables in PowerShell is an essential skill, as these variables are used by the operating system and applications to determine various settings and behaviors. In PowerShell, you can create, modify, and remove environment variables, which can be scoped to the current process, the user, or the entire system. Here’s a comprehensive guide on how to handle environment variables in PowerShell.

Creating an Environment Variable

  1. Set a New Environment Variable for the Current PowerShell Session:

To set an environment variable that only lasts for the duration of your current PowerShell session, use the following syntax: $env:VARIABLE_NAME = "value"

  • For example, to create a new environment variable named TEMPVAR with the value tempValue:   $env:TEMPVAR = "tempValue"

2. Create a System-Wide Environment Variable:

  • To set the environment variable persistently, affecting the entire system (requires administrative rights):
    [System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", "value", [System.EnvironmentVariableTarget]::Machine)
    • For instance, creating a machine-scoped variable MYVAR: [System.Environment]::SetEnvironmentVariable("MYVAR", "myValue", [System.EnvironmentVariableTarget]::Machine)

3. Create an Environment Variable for the Current User:

  • To set an environment variable that persists for your user profile: [System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", "value", [System.EnvironmentVariableTarget]::User)

Reading Environment Variables

  1. Get the Value of an Environment Variable:
    • You can retrieve the value of an environment variable using:   Get-ChildItem Env:VARIABLE_NAME
      • Or simply by:   echo $env:VARIABLE_NAME

Removing an Environment Variable

  1. Remove an Environment Variable from the Current Session:

    • To delete an environment variable in the current PowerShell session:   Remove-Item Env:VARIABLE_NAME
      • For example, removing TEMPVAR:   Remove-Item Env:TEMPVAR

2. Delete a Persistent Environment Variable:

  • To permanently remove a user or system variable:   [System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", $null, [System.EnvironmentVariableTarget]::Scope)
    • For example, to remove MYVAR from the machine scope:   [System.Environment]::SetEnvironmentVariable("MYVAR", $null, [System.EnvironmentVariableTarget]::Machine)

Examples and Use Cases

  1. Creating a New Path Variable:

    • To add a new directory to the PATH environment variable in the current session:   $env:PATH += ";C:MyNewPath"
  2. Using Environment Variables to Store Configuration:

    • Set an azure_resource_group environment variable for use in a script:   $env:azure_resource_group = "MyResourceGroup"
  3. Changing the PowerShell Profile Path:

    • Modify the PSModulePath environment variable to include a custom module directory:   $env:PSModulePath += ";C:MyCustomModules"

Notes on Environment Variables in PowerShell

  • Scopes: Environment variables in PowerShell can be set in three scopes: Process, User, and Machine. Process-scoped variables exist only in the current PowerShell session, while user and machine-scoped variables persist outside of the current process.
  • Persistence: Changes to user or machine-scoped variables may require a restart of the PowerShell session or the system to take effect.
  • Environment Provider: PowerShell provides the Environment Provider which allows you to work with environment variables using paths, much like file system paths.
  • Importance: Environment variables are essential as they can influence the behavior of processes or the PowerShell session itself. They are commonly used to store paths, configuration settings, and other important data.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.