Futzing With System Variables
$Env:PATH is a common bane - you have a hundred machines, and you end up with fifty different $Env:PATH values. Here's a quick way to audit them, and to set them remotely. But be careful: just like with superpowers, this comes with great responsibility.
function Get-SystemVariable {
param (
[Parameter(
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)][String[]]$ComputerName = @(),
[Parameter( Position = 1 )][string]$Name
);
process {
if (!$Name) { Out-Error "-name not specified. Required."; }
foreach ($computer in $ComputerName) {
$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT * FROM Win32_Environment WHERE username='<system>'";
$computer | Select-Object -Property @{
n = 'ComputerName';
e = { $_; }
}, @{
n = $name.ToUpper();
e = { ($wmi | ? { $_.Name -eq $name }).VariableValue; }
}
}
}
}
function Set-SystemVariable {
param (
[Parameter(
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)][String[]]$ComputerName = @(),
[Parameter( Position = 1 )][string]$Name,
[Parameter( Position = 2 )][string]$Value
);
process {
if (!$Name) { Out-Error "-name not specified. Required."; }
if (!$Value) { Out-Error "-value not specified. Required."; }
foreach ($computer in $ComputerName) {
$temp = (Get-SystemVariable -ComputerName $computer -Name $Name).$Name
SetX.exe -s $computer -m $Name $Value | Out-Null
Get-SystemVariable -ComputerName $computer -Name $Name | Select-Object -Property *, @{
n = 'OldValue';
e = { $temp; }
}
}
}
}