How To Work With Environment Variables In PowerShell
Think you can do better? Show us!
Working with environment variables can be a real pain sometimes. In this Snip we’ll learn how to create, change, remove environment variables with PowerShell. We’ll take a look the environment provider and how we can interact with it like we are used to interacting with the filesystem with commands like Get-ChildItem and Get-Item. A common problem is updating the PATH environment variable. We’ll see why the changes don’t remain persistent and how we can work around it so we don’t need to open it in every new PowerShell session! Last we’ll talk about environment variables that store preferences, like PSModulePath. Finally we’ll see how we can use [System.Environment] to make changes as well. Prerequisites include: N/A
folks today, we're going to learn how to work with environment variables in powershell. I'll be doing this snip on windows, however you can follow along with powerShell core on Mac or Linux. Just be aware that the environment variables will differ a bit on those operating systems. One of the ways you can read and modify environment variables in powershell is with the environment provider. This experience is similar to other providers like the registry or file system providers. You can see the environment provider with get PS provider as you can see we have a provider for environment with a PS drive of env or E N V for the environment variables, you can see that PS drive as well by running get PS drive. You can list items in the environment PS drive by using the cmdlets that contain the item noun using get child item will go ahead and look at the value of the computer name environment variable. Once we run it, we can see the current value of the computer name environment variable due to the fact that environment variables do not have children, both get item and get child item will return the same output. Since we're currently in a file system PS drive. We can change our location to the environment. PS drive this way we won't need to specify the ENV backslash in our commands. Now, if we want to list the computer name environment. Variable we can do so without including the ENV backslash to list. All the environment variables, we can simply run get child item in the root of this drive. You can also view environment variables using the expression parser in powershell to use the expression parser for environment variables. You would use dollar sign ENV dollar sign indicating a variable and ENV being the name of the PS drive using the syntax. We can look at the value of computer name. This is one of the more common ways people tend to access environment variables in powershell. Now look at changing environment variables with powershell when you chang an environment variable in powershell, it will only affect the current session using the path environment variable as our example will add a new directory path to the path environment variable before we do, we'll go ahead and take a look at the current value of our path environment variable? A quick note, pads in the path environment variable are separated by semicolons. I've added. The dash split on semi colons to make the output easier to read. Now I will add a new directory path to the path environment variable we’ll add C temp. You can see this change reflected if we view the value path again C temp is now at the bottom of the list. Let's restart the powershell integrated console in VS code you'll see once we do in Redisplay. The value of the path environment variable the new path, we added is no longer there. As you can see our path is no longer present as we saw earlier we can also make these changes using the environment. PS drive in the item cmdlets we’ll, use get item to display the value of the path environment variable we can now use set item to modify the value of the path environment variable we’ll add C temp back in. And now using the get item command again we can see our change. Now we'll use our powershell profile to work around environment variables, resetting each time we restart powerShell, like before we're going to add C temp to our path environment variable and as you can see it's currently not present we’ll edit our powerShell profile and add a line that will update the path environment variable and we start PowerShell since we're using VS code as our editor in the Snip we’ll go ahead and make the change here. We’ll save our powershell profile and go ahead and restart our powershell integrated console again. Now when powershell loads and our profile loads. It will update our path environment variable to add the C temp path to it now that we restarted PowerShell when we view the value of the path environment variable we can see C temp at the bottom. You can also create and delete environment variables with powershell. Will use the new item commandlet to create a new environment, variable named techsnips now that it's created we can see it and its value we’ll go ahead and delete it now and as you can see it's gone. PowerShell stores, some of its preferences in environment variables when we view the value of PS module path. We can see all the locations powershell will search for modules on the system. We can add a new path that the PS module path environment variable again for this example we’ll add C temp. Now, if we were to put powershell modules into C temp powershell would be able to locate and import them. Much like the path environment variable the settings don't persist unless you use. Some of the methods in this snip to make it persistent. You can also use the net system environment class to view and change environment variables using this method is an alternative to changing them persistently that doesn't involve modifying your PowerShell profile using the static method get environment variables, we can. View all the environment variables. You can also use the get environment. Variable static method to view the value of a single environment variable. The static method takes 2 parameters. The first one being the environment, variable to view and the second one being the scope, either user or machine. To view the path environment variable for the user we specify path and user. And to view the path environment variable for the machine we would specify path and machine. We can use the system environment set environment variable static method to change and update environment variables as well we’ll update our user’s path environment variable to add C Techsnips to the path first will get the current value of the path environment variable and store in a powershell variable. Now we use the set environment variable and append the new path to the end similar to before the first parameter is the environment, variable to update the second parameter is the value to change it to and the last parameter is the scope. Now we can see the new path of C techsnips added to our path environment variable even when I restart powershell using this method the changes will persist. Since I'm not running as an administrator. If I try to change an environment variable for the machine. I'll get an error. Keep this in mind if you need to make a change to an environment variable that's under the machine scope. If you ever get stuck there's a detailed about help file which covers everything. We've talked about in this snip and a little more. Now you have the tools you need to tackle environment variables with powershell.