Files
management-scripts/Cleanup-SystemPath.ps1
T

36 lines
1.0 KiB
PowerShell
Raw Normal View History

2022-03-09 12:59:22 -05:00
## Get contents of SYSTEM PATH environment variable
2020-08-18 16:20:20 -04:00
$REG_ENVVAR = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
2020-09-28 15:14:18 -04:00
$CURRENT_PATH = (Get-ItemProperty -Path $REG_ENVVAR -Name Path).Path
2020-08-18 16:20:20 -04:00
$NEW_PATH = $null
$REMOVE_PATH =$null
## Verify each path
Foreach ( $path in $CURRENT_PATH.Split(";") )
{
If ( (Test-Path $path) -or ($path -like "*%*%*") )
{
If ( $NEW_PATH ) { $NEW_PATH += ";${path}" }
Else { $NEW_PATH = $path }
}
Else
{
If ( $REMOVE_PATH ) { $REMOVE_PATH += ";${path}" }
Else { $REMOVE_PATH = $path }
}
}
2023-10-05 13:11:38 -04:00
Write-Output "Removed Paths:`n`r${REMOVE_PATH}"
2020-08-18 16:20:20 -04:00
2020-09-28 15:14:18 -04:00
Try { Set-ItemProperty -Path $REG_ENVVAR -Name Path -Value $NEW_PATH }
Catch
{
2023-10-05 13:17:18 -04:00
Write-Error $_.Exception.Message
Write-Error "Failed to cleanup system path, using original values"
2020-09-28 15:14:18 -04:00
Try { Set-ItemProperty -Path $REG_ENVVAR -Name Path -Value $CURRENT_PATH }
Catch
{
2023-10-05 13:17:18 -04:00
Write-Error $_.Exception.Message
Write-Error "This is not good... !! DO NOT RESTART UNTIL SYSTEM PATH IS FIXED !!"
2020-09-28 15:14:18 -04:00
Exit
}
}