Getting and setting environment variables using powershell (on windows) ע
Get-ChildItem Env:
Get-ChildItem Env: | Where-Object {$_.Name -like 'Program*'}
for example get the homepath
value
$env:homepath
PATH
) $env:path -split ';'
PS C:\Users\Sagiv> $env:names = 'sagiv'
PS C:\Users\Sagiv> $env:names += ';Sagiv Barhoom'
PS C:\Users\Sagiv> $env:names -split ';'
sagiv
Sagiv Barhoom
On Windows, environment variables can be defined in three scopes:
Lets set the environment persistently so they will remain even when close the currrent session.
Use [System.Environment]
class with the SetEnvironmentVariable
method.
[Environment]::SetEnvironmentVariable('names','Sagiv;Sagiv Barhoom', 'user')
Note: you need premmitions to edit the registry - so you might wat to run this as admin
[Environment]::SetEnvironmentVariable('names','Sagiv;Sagiv Barhoom', 'Machine')