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
PS D:\> [Environment]::GetEnvironmentVariables()
Only the user part
PS D:\> [Environment]::GetEnvironmentVariables("user")
and only the system/machine part
PS D:\> [Environment]::GetEnvironmentVariables("machine")
A single variable can be read by
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
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