@@ -34,7 +34,7 @@ function Add-ToSystemPath {
3434 return $principal.IsInRole ([Security.Principal.WindowsBuiltInRole ]::Administrator)
3535 }
3636
37- function Normalize -DirectoryPath {
37+ function Format -DirectoryPath {
3838 param (
3939 [Parameter (Mandatory = $true )]
4040 [string ]$Path
@@ -59,7 +59,7 @@ function Add-ToSystemPath {
5959 return $Path.Trim ().TrimEnd(' \' ).ToUpperInvariant()
6060 }
6161
62- function Split-PathEntries {
62+ function Split-PathEntry {
6363 param (
6464 [AllowNull ()]
6565 [string ]$PathValue
@@ -76,30 +76,35 @@ function Add-ToSystemPath {
7676 }
7777
7878 function Update-CurrentSessionPath {
79+ [CmdletBinding (SupportsShouldProcess )]
80+ param ()
81+
7982 $machinePath = [Environment ]::GetEnvironmentVariable(' Path' , ' Machine' )
8083 $userPath = [Environment ]::GetEnvironmentVariable(' Path' , ' User' )
8184
8285 $combined = @ ()
8386
8487 if (-not [string ]::IsNullOrWhiteSpace($machinePath )) {
85- $combined += Split-PathEntries - PathValue $machinePath
88+ $combined += Split-PathEntry - PathValue $machinePath
8689 }
8790
8891 if (-not [string ]::IsNullOrWhiteSpace($userPath )) {
89- $combined += Split-PathEntries - PathValue $userPath
92+ $combined += Split-PathEntry - PathValue $userPath
9093 }
9194
92- $env: Path = ($combined -join ' ;' )
95+ if ($PSCmdlet.ShouldProcess (' env:Path' , ' Update' )) {
96+ $env: Path = ($combined -join ' ;' )
97+ }
9398 }
9499
95100 if (-not (Test-IsAdministrator )) {
96101 throw ' Administrator privileges are required to modify the system PATH. Re-run PowerShell as Administrator.'
97102 }
98103
99- $normalizedPathToAdd = Normalize - DirectoryPath - Path $PathToAdd
104+ $normalizedPathToAdd = Format -DirectoryPath - Path $PathToAdd
100105
101106 $currentMachinePath = [Environment ]::GetEnvironmentVariable(' Path' , ' Machine' )
102- $currentEntries = Split-PathEntries - PathValue $currentMachinePath
107+ $currentEntries = Split-PathEntry - PathValue $currentMachinePath
103108
104109 $existingKeys = @ {}
105110 foreach ($entry in $currentEntries ) {
@@ -109,10 +114,10 @@ function Add-ToSystemPath {
109114 $newKey = Get-NormalizedPathKey - Path $normalizedPathToAdd
110115
111116 if ($existingKeys.ContainsKey ($newKey )) {
112- Write-Host ' '
113- Write-Host ' Path already exists in the system PATH:' - ForegroundColor Yellow
114- Write-Host " $normalizedPathToAdd "
115- Write-Host ' '
117+ Write-Information ' '
118+ Write-Information ' Path already exists in the system PATH:'
119+ Write-Information " $normalizedPathToAdd "
120+ Write-Information ' '
116121 Update-CurrentSessionPath
117122 return
118123 }
@@ -127,14 +132,14 @@ function Add-ToSystemPath {
127132 [Environment ]::SetEnvironmentVariable(' Path' , $newMachinePath , ' Machine' )
128133 Update-CurrentSessionPath
129134
130- Write-Host ' '
131- Write-Host ' Added to system PATH:' - ForegroundColor Green
132- Write-Host " $normalizedPathToAdd "
133- Write-Host ' '
134- Write-Host ' The machine PATH has been updated.' - ForegroundColor Green
135- Write-Host ' The current PowerShell session PATH has also been refreshed.' - ForegroundColor Green
136- Write-Host ' '
137- Write-Host ' New processes will see the updated PATH immediately.' - ForegroundColor White
138- Write-Host ' Some already-running applications may need to be restarted.' - ForegroundColor White
139- Write-Host ' '
135+ Write-Information ' '
136+ Write-Information ' Added to system PATH:'
137+ Write-Information " $normalizedPathToAdd "
138+ Write-Information ' '
139+ Write-Information ' The machine PATH has been updated.'
140+ Write-Information ' The current PowerShell session PATH has also been refreshed.'
141+ Write-Information ' '
142+ Write-Information ' New processes will see the updated PATH immediately.'
143+ Write-Information ' Some already-running applications may need to be restarted.'
144+ Write-Information ' '
140145}
0 commit comments