Adding a directorty to PATH or PSModulePath
The following simple PowerShell script adds the current directory to a user's PSModulePath:
set-strictMode -version latest
$addedDirectory=$pwd
$psModules_ = [environment]::getEnvironmentVariable('PSModulePath', 'user')
if ($psModules_ -eq $null) {
$psModules = ($addedDirectory)
}
else {
$psModules = $psModules_ -split ';'
$psModules = $psModules + $addedDirectory
}
[environment]::setEnvironmentVariable('PSModulePath', ($psModules -join ';') , 'user')
Changing the value in the registry
If the value of an environment variable is changed directly in the registry, a
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment")
should be executed in order for the changes to take effect.
In
PowerShell, this is possible with something like the following
( add-type -passThru -name dummy -memberDefinition '
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(
IntPtr hWnd,
UInt32 uMsg,
UIntPtr wParam,
String lParam // Note, this parameter is actually an IntPtr. This construct passes `Environment` as a string.
);
'
)::SendMessage(0xffff, 0x001A, [UIntPtr]::Zero, "Environment")