I've run into a strange problem in that I've set Path variables in Windows, after a reboot they disappear until I go look at the Environment variable settings.

I can restore them by simply doing:

System Properties => [Environment Variables] => [OK]

...then they are set again!

Note: If I type [SET], I can see them in my Path

Example:

I have an addition to my path which points to: %USERPROFILE%\Documents\WindowsPowerShell\Scripts

In ^^that^^ dir I have a file myscript.ps1.

If I open PowerShell after booting up, I can type "my [TAB]" and it will not find the script.

After Opening the Environment Variables Dialog and selecting [OK], restart PowerShell, I can do the same "my [TAB]" and autofill "myscript.ps1" without any issue.

Anyone know how to resolve this?

  • I have the same problem, I add a environment variable for the user, reboot, and then the environment variable is gone. I need to manually add the variable again in System Properties => Environment Variables. I have no clue what causes this behavior, it used to work just fine. – André Mar 31 '17 at 9:12
  • By chance do you have that set as the only value in the section "User variables for x" (where x = user name)? Because in that case it ought to be ";%USERPROFILE%\Documents\WindowsPowerShell\Scripts" (w/out quotes"). Beyond that I'd guess that you have a syntax error somewhere in the variable. – BrianC Mar 31 '17 at 22:17
  • Thanks BrianC and @JosefZ I moved the vairable from "System Variables" to "User variables from X" then validated it with script posted int he answer. Saw that a path to "C:\Program Files (x86)\ATI Technologies\"... failed to validate. Removed it. Now all paths work after a reboot! – Kareem Apr 3 '17 at 14:43
up vote 1 down vote accepted

This isn't a real answer (too long and too complex for a comment). I will delete it if it proves false way.

Please use the following naive script to check whether your path setting is formally valid:

Function myTest-Path {
Param( [Parameter(Mandatory=$true)] [string] $PathString )
if ( $PathString -match '%' ) { $PathString = (. cmd /c "echo($PathString")}
if ( Test-Path -Path $PathString -IsValid) {
        [string] (Test-Path (Join-Path -ChildPath '' -Path $PathString))
    } else { '!wrong' }
}

### Windows path validity check ###
Write-Host '$env:Path splitted' -ForegroundColor Yellow
$aux = $env:Path
$aux -split ";" | ForEach-Object {'{0,6} "{1}"' -f (myTest-Path $_), $_}

Write-Host 'HKCU:\Environment\ Path splitted' -ForegroundColor Yellow
$aux = ([Microsoft.Win32.Registry]::CurrentUser.
    OpenSubKey("environment")).
    GetValue("Path",$False, 
        [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$aux -split ";" | ForEach-Object {'{0,6} "{1}"' -f (myTest-Path $_), $_}

Write-Host 'HKLM:\SYSTEM\…\Environment\ Path splitted' -ForegroundColor Yellow
$aux = ([Microsoft.Win32.Registry]::LocalMachine.
    OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment")).
    GetValue("Path",$False, 
        [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$aux -split ";" | ForEach-Object {'{0,6} "{1}"' -f (myTest-Path $_), $_}

Write-Host '$env:Path duplicities' -ForegroundColor Yellow
$auxArr = $env:Path -split ";"
for ($i = 0; $i -le $auxArr.Count; $i++) {
    for ($j = $i+1; $j -le $auxArr.Count -1; $j++) {
        try {
            if ( (Join-Path -ChildPath '' -Path $auxArr[$j] -ErrorAction Stop) -eq 
                 (Join-Path -ChildPath '' -Path $auxArr[$i] -ErrorAction Stop) )   {
                    '{0,4} {1,4} "{2}"' -f $i, $j, $auxArr[$i]
            }
        } catch {
            ### Write-Host "$i $j invalid folder name in `$env:Path" -ForegroundColor Red
        }
    }
}
  • Thanks @BrianC and JosefZ I moved the vairable from "System Variables" to "User variables from X" then validated it with script posted int he answer. Saw that a path to "C:\Program Files (x86)\ATI Technologies\"... failed to validate. Removed it. Now all paths work after a reboot! – Kareem Apr 3 '17 at 14:43

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.