From 504ee7f05c88c82a0c21dacc4cdc546c7d938c9d Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Mon, 22 Feb 2021 00:24:24 -0800 Subject: [PATCH] Updated to ensure PSToolbox path appears at the beginning of the PSModulePath environment variable --- Setup.ps1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Setup.ps1 b/Setup.ps1 index c001fe5..9062fa8 100644 --- a/Setup.ps1 +++ b/Setup.ps1 @@ -32,18 +32,19 @@ Function Script:Set-EnvironmentVariable { if($Scope -eq 'Machine') { $scopeArgs = '/M' } - setx.exe $Name $Value $scopeArgs | Out-Null + + [System.Environment]::SetEnvironmentVariable($Name,$Value,[System.EnvironmentVariableTarget]::Machine) Set-Item -Path Env:$Name -Value $Value } $PSToolboxPath=Join-Path $PSScriptRoot Modules -if($env:PSModulePath -notlike "*$PSToolboxPath*") { - Script:Set-EnvironmentVariable -Name 'PSModulePath' -Value "$PSToolboxPath;$env:PSModulePath" +if($env:PSModulePath -notlike "$PSToolboxPath*") { + $UpdatedPSModulePath = "$PSToolboxPath;$(($env:PSModulePath -split ';' | Where-Object{ $_ -ne 'C:\Git\PSToolbox\Modules' }) -join ';')" + Script:Set-EnvironmentVariable -Name 'PSModulePath' -Value $UpdatedPSModulePath if($PSToolboxPath -notin ($env:PSModulePath -split ';')) { - Write-Host -foreground Cyan $PSToolboxPath #NOTE: This does not test the change from [System.Environment]::SetEnvironmentVariable is permanent. - throw "PSModulePath ('$env:PSModulePath') is not set with $PSToolboxPath" + throw "PSModulePath ('$env:PSModulePath') is not set with $PSToolboxPath" } }