Windows PowerShell Tip: Creating and Modifying Environment Variables

Please Visit: http://ift.tt/1ajReyV



Creating and Modifying Environment Variables

http://ift.tt/1icgygB

$Env:os

[environment]::GetEnvironmentVariable("Sample","User")

The User type is an environment variable tied to a user profile. You can also have Machine environment variables, which are tied to the computer as a whole, and Process environment variables, which are restricted to a single process.

$env:TestVariable = "This is a test environment variable."

Set process-level environment variable

$env:TestVariable = "This is a test environment variable."

Like we said, though, that approach only works for process-level environment variables. To create more permanent environment variables (i.e., user-level or machine-level) you need to use the .NET Framework and the SetEnvironmentVariable method

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

One thing to watch out for: when we used SetEnvironmentVariable to create a new user- or machine-level environment variable that variable didn’t always show up when we ran this command in Windows PowerShell:



Get-ChildItem Env:

Or at least it didn’t show up until we restarted PowerShell. (Or started up a new instance of PowerShell.) However, we could retrieve the value of the new variable at any time by using this command:

[Environment]::GetEnvironmentVariable("TestVariable","User")



Deleting Environment Variables

Remove-Item Env:\TestVariable

[Environment]::SetEnvironmentVariable("TestVariable",$null,"User")



#PSTip Refresh the PSModulePath environment variable without re-opening console

$env:PSModulePath = [System.Environment]::GetEnvironmentVariable("PSModulePath","Machine")

http://ift.tt/1e8R0C0



from Google Plus RSS Feed for 101157854606139706613 http://ift.tt/1icgygB

via LifeLong Community

No comments:

Post a Comment