• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Sco Openserver
    • Sco Unix Support
    • SCO Openserver Installation ISOs
    • SCO Openserver Binaries
    • Add a Hard Drive to an Openserver 5 Virtual Machine
    • Install SCO Vision SQL-Retriever ODBC Driver on Windows 10
    • License Expired on Virtual SCO Openserver Installation
    • How to reset the root password on SCO Openserver 5
  • Scripting
    • PowerShell
      • Add leading zeros
      • Check if a File Exists
      • Grep with Powershell
      • Create Environment Variables
      • Test for open Ports
      • Append to a Text File
    • VBScript
      • Check if a File Exists
      • lpad and rpad functions
      • Windows Update E-Mail Notification
  • Office 365
    • Connect to Office 365 with PowerShell
    • Add or remove an email alias using Powershell
    • Change Primary email address of Active Directory user
    • How to hide an AD user from the Global Address List
    • How to hide mail contacts from the Global Address List
    • Change the primary email address for an account with PowerShell
    • Change Primary email address of an AD User
    • Grant a single user access to access to all calendars
    • Forward email to an external address using Powershell
    • Convert shared mailbox to user mailbox with Powershell
  • Get in Touch
  • About Me
    • Privacy Policy

Use PowerShell to Set Environment Variables

March 11, 2019 by Paulie 3 Comments

Setting environment variables in PowerShell is easy. This post will show you how to create a PowerShell environment variable which is scoped:

  • Locally to your current PowerShell session.
  • To your user profile and available to all other processes that you execute
  • To the machine, and accessible to all processes that run on that system.

Set a locally scoped Environment Variable

To create an environment variable that is local to your current PowerShell session, simply use:

$env:SiteName = 'tachytelic.net'

Then you can check the value of the environment variable with this code:

Get-ChildItem Env:SiteName

Image showing how to set a local Environment Variable in Powershell

This method is fine, but the variable will vanish when the PowerShell process ends.

Set an Environment Variable scoped to the User

To set an environment variable which will be available to all processes that your account runs, use the following:

[System.Environment]::SetEnvironmentVariable('siteName','tachytelic.net',[System.EnvironmentVariableTarget]::User)

Now you can see the variable is set from the Environment variables section of the system properties:

Image showing currently set User Environment Variables

Set an Environment Variable scoped to the Machine

To create an environment variable visible to every process running on the machine:

[System.Environment]::SetEnvironmentVariable('siteName','tachytelic.net',[System.EnvironmentVariableTarget]::Machine)

Note: This command will probably fail unless you run PowerShell as an administrator.

The new PowerShell System environment variable is visible:

Image showing system environment variable added by PowerShell

It’s quite interesting that the PowerShell environment variables are stored in a drive, which you can access using:

Set-Location Env:
Get-ChildItem

For more information on how PowerShell environment variable work, read the Microsoft documentation here.

Related

Filed Under: Scripts & Utilities Tagged With: Powershell

Reader Interactions

Comments

  1. Richard says

    May 21, 2020 at 8:49 am

    Really useful, thanks

  2. charles a ross says

    August 12, 2020 at 2:32 pm

    The latter 2 will only work if you are running admin shell.

  3. Jonathan Tyler says

    September 18, 2020 at 1:14 pm

    Hi, your link to additional information at the end of the article does not point to a valid address.

Leave a Reply Cancel reply

Primary Sidebar

Link to my LinkedIn Profile
Go to mobile version