Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
DevOps

How to Set Environment Variables with Powershell?

Introduction

PowerShell is a well-known command line interface. This means that you can execute commands with PowerShell. This unique software program helps to control and automate tasks on the computer. The process to set environment variables in PowerShell is a bit complex, which is why we have created this easy-to-understand guide. You utilize this piece of writing to grasp the concept of environment variables and learn the steps to edit them through quick steps. We have tried to explain every aspect of these variables and PowerShell by digging deeper into the checking and setting process. We will start by reading about the definition of the environment variable first. 

Environment Variables: Definition

The environment variable is a unique setting that can support many different tasks. Whether you want to place a particular location, store important credentials, or customize the behavior of a program, this variable can do it all for you. Let’s understand these variables through a simple analogy of a notebook. 

So, let’s assume that the computer is a big notebook that consists of different sections. Now, these sections are labeled with sticky notes for the convenience of the reader. These sticky notes are similar to environment variables. Here, the notes contain important reminders, just like credentials and passwords, along with essential configurations. The unique software, PowerShell, is used to translate the information written attached with a variable. Moreover, it can act like a magic pen that can edit sticky notes and rewrite content. This leads to effective communication with the computer system. 

Check Environment Variables

It is very important to check these variables as much important information is placed inside them. If there is any flow issue or misconfiguration, then the first thing to do would be to check the environment variable through PowerShell. Now that you understand these variables’ basic concepts, let’s examine how to check them. 

– Search for specific variables

  1. Using $env: You can check the variable with a specific name through this method. Use the following command to do so: 
Search for specific variables
  1. Using Get-Item: You can easily extract the value of a particular variable using this command. 
Search for specific variables
  1. Using Get-Content : If there is a requirement to get the content of a particular variable, then the following command can do so: 
Search for specific variables

– Search for broad variables

Using the $env: Drive: With this command, you can view all the variables available for the current session. 

Search for specific variables

Using the Get-ChildItem Cmdlet: This is another method to list all the environment variables in the current session. 

Search for specific variables

Setting Temporary Environment Variables in Powershell

In PowerShell, you can set environment variables temporarily using the “$env” and “set-item” commands. Here’s how you can do it:

  • $env approach: 
Setting Temporary Environment Variables in Powershell
  • Set-Item cmdlet approach: 
Setting Temporary Environment Variables in Powershell

Unsetting Environment Variables

A user can easily unset the environment variable in PowerShell using the following methods: 

  1. Using Remove-Item Cmdlet: With the Remove-Item cmdlet, the user can remove environment variables set using PowerShell’s $env: drive. Here, the user will specify the path to the environment variable to be removed and prefixed with Env:. For example, Remove-Item -Path Env:MY_VARIABLE would remove the environment variable named MY_VARIABLE.
Unsetting Environment Variables
  1. Using Remove-ItemProperty Cmdlet: When environment variables are set using the Registry with Set-ItemProperty, you can remove them using the Remove-ItemProperty cmdlet. This command operates on the Windows Registry and requires specifying the registry path where the variable is stored. For instance, Remove-ItemProperty -Path ‘HKCU:\Environment’ -Name MY_VARIABLE would remove the environment variable MY_VARIABLE from the user’s Registry.
Unsetting Environment Variables
  1. Using $env: VariableName: You can also unset environment variables directly by setting their value to $null. This method is convenient for removing variables set during the current PowerShell session. For example, $env:MY_VARIABLE = $null would effectively remove the MY_VARIABLE environment variable from the session. It’s a quick and straightforward way to clear variables without using cmdlets.
Unsetting Environment Variables

Also Read: List Process Linux – How to Check Running Processes?

Summing Up

The concept of environment variables and the way to manipulate them in PowerShell is crucial to managing the system. These unique variables serve as key settings, storing essential information like file paths, credentials, and program configurations. PowerShell is a fantastic tool to check and set these environment variables. PowerShell is highly supportive of streamlining workflows and troubleshooting issues effectively.

With PowerShell’s commands, such as $env: and Set-Item, users can easily set temporary environment variables for the duration of their session. Moreover, the ability to unset environment variables using commands like Remove-Item and Remove-ItemProperty ensures users can maintain a clean and organized environment, removing unnecessary clutter or outdated configurations.

We hope this article has helped you gain insight into the fundamentals of environment variables and learn practical techniques for manipulating them within PowerShell.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles