Hi,
I’m calling Invoke-Command to run [Environment]::SetEnvironmentVariable(“PSModulePath”, $old + “;c:\test”, “Machine”) on a remote machine.
With a RDP session running on the same machine, starting a new PowerShell prompt, the $env:PSModulePath is not updated, instead ONLY the c:\windows\system32\windowspowershell\v1.0\modules path is shown.
If I open “control panel”, the system PSModulePath is correct. If I then save the path from “Control panel”, a new PowerShell prompt will show the $env:PSModulePath correctly. A server boot also fixes the problem.
If I run the commands inside the Invoke-Comand locally (using the rdp session) it works as expected, the path is updated when I open a new prompt.
So,
- Why does not the PSModulePath update as expected?
- Any suggestions on how to get the path to update as expected?
- Are someone able to reproduce this? I have tried on several machines, but they are all similar.
The code:
Invoke-Command -ComputerName $computer -ArgumentList $localTarget,$VerbosePreference -ScriptBlock {
param(
[string]$localTarget,
[System.Management.Automation.ActionPreference] $VerbosePreference
)
$currentValue = [Environment]::GetEnvironmentVariable(“PSModulePath”, “Machine”)
if (!($currentValue.Contains($localTarget)))
{
$currentValue += “;” + $localTarget
Write-Verbose ” Adding $localTarget to PSModulePath”
[Environment]::SetEnvironmentVariable(“PSModulePath”, $currentValue, “Machine”)
} else {
Write-Verbose ” $localTarget Already in PSModulePath”
}
}