Creating and Modifying Environment Variables

Hey folks. I am referencing this article

I want to create an environment variable for %USERPROFILE%\AppData\Local\Apps directory.

What value / data-type must you specify for the “Test value.” parameter when using the SetEnvironmentVariable method.


You’re referring to:

[Environment]::SetEnvironmentVariable(“TestVariable”, “Test value.”, “User”)


They’re all strings.

Yes that method. Oh so it’s just a simple string %USERPROFILE%\AppData\Local\Apps\2.0

Don do you know if it is possible to delete files in an environment variable? by piping Remove-Item or similar?

Anything involving a PSDrive, including ENV:, will only affect the current process. But - and I’ll explain my process here, in case that helps you - I Googled “.NET Environment Class.” That got me to, which is the class you were using to set a variable.

I clicked on Methods, which got me to

I didn’t see a “RemoveEnvironmentVariable” method, which made me sad. So I looked at the docs for SetEnvironmentVariable. Specifically,

That told me that the method could also DELETE a variable. Winner! You just pass $null as the value, and if the variable exists, it gets deleted.

Hope that helps.

Thanks! To verify it’s deleted you can also use the method [Environment]::GetEnvironmentVariable(“Test Value.”)

I ran into an ‘issue’ / limitation while using this method. I needed to create an environment variable for the local app-data directory %USERPROFILE%\AppData\Local\Apps\2.0</code>. I tried creating a variable which used the $env:USERPROFILE for the target parameter, to access user profiles on remote machines. as a work around I used the following code to access the local app-data!

new-item -path $env:UserProfile\AppData\Local\Apps\2.0\ -itemtype directory -force