Hi,
powershell can set,edit or delete environment variables as well as cmd.exe command line interpreter.
Powershell has no command let for this. But the .NET framework has an appropriate class [Environment].
Some examples. The environment of an user contains two parts: The user and the system part.
All variables can be shown by
1 | PS D:\> [Environment]::GetEnvironmentVariables() |
Only the user part
1 | PS D:\> [Environment]::GetEnvironmentVariables("user") |
and only the system/machine part
1 | PS D:\> [Environment]::GetEnvironmentVariables("machine") |
A single variable can be read by
1 | PS D:\> $myPowershellModulePathes=[Environment]::GetEnvironmentVariable("PSModulePath ","user") |
The method to set an environment variable is [Environment]::SetEnvironmentVariable().
For example: You want to add a additional user path, D:\MyModules, for powershell modules. Powershell reads the module pathes from the PSModulePath environment variable.
Get the current variable and add the new path
1 2 3 4 5 6 7 8 9 10 | PS D:\> $myPowershellModulePathes=[Environment]::GetEnvironmentVariable("PSModulePath ","user")PS D:\>if( [string]::IsNullOrEmpty($myPowershellModulePathes ){ [Environment]::SetEnvironmentVariable("PSModulePath ","D:\MyModules","user")}else{ [Environment]::SetEnvironmentVariable("PSModulePath ", ([string]::Join(";",@($myPowershellModulePathes,"D:\MyModules"))) ,"user")} |
Michael