## Get contents of SYSTEM PATH environment variable
$REG_ENVVAR = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
$CURRENT_PATH = (Get-ItemProperty -Path $REG_ENVVAR -Name Path).Path
$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 }
    }
}

LogMsg "Removed Paths:`n`r${REMOVE_PATH}"

Try { Set-ItemProperty -Path $REG_ENVVAR -Name Path -Value $NEW_PATH }
Catch
{
    LogErr $_.Exception.Message
    LogErr "Failed to cleanup system path, using original values"
    Try { Set-ItemProperty -Path $REG_ENVVAR -Name Path -Value $CURRENT_PATH }
    Catch
    {
        LogErr $_.Exception.Message
        LogErr "This is not good... !! DO NOT RESTART UNTIL SYSTEM PATH IS FIXED !!"
        Exit
    }
}