Windows 10 Environment Path Variables Missing after reboot
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:
### Windows path validity check ###
Function myTest-Path {
Param( [Parameter(Mandatory=$false)] [string] $PathString='' )
if ( '' -eq $PathString )
{
'!empty'
} else {
if ( $PathString -match '%' ) {
$PathString = (. cmd /c "echo($PathString")
}
if ( Test-Path -Path $PathString -IsValid) {
[string] (Test-Path (Join-Path -ChildPath '' -Path $PathString))
} else { '!wrong' }
}
}
Write-Host '### Windows path validity check ###' -ForegroundColor Yellow
Write-Host 'checking $env:Path split' -ForegroundColor Yellow
$aux = $env:Path -replace ';$' # remove trailing semicolon
$aux -split ";" |
ForEach-Object {
[PSCustomObject]@{
check = '$env:Path';
valid = myTest-Path -PathString "$_";
path = "$_"
}
}
Write-Host 'checking HKCU:\Environment\ Path split' -ForegroundColor Yellow
if ( (Get-Item 'HKCU:\Environment\').GetValue('Path') ) {
$aux = ([Microsoft.Win32.Registry]::CurrentUser.
OpenSubKey("Environment")).
GetValue("Path",$False,
[Microsoft.Win32.RegistryValueOptions]::
DoNotExpandEnvironmentNames) -replace ';$'
$aux -split ";" | ForEach-Object {
[PSCustomObject]@{
check = 'HKCU:Path';
valid = myTest-Path -PathString "$_";
path = "$_"
}
}
} else {
Write-Host 'HKCU:\Environment\ Path does not exist' -ForegroundColor Cyan
}
Write-Host 'checking HKLM:\SYSTEM\…\Environment\ Path split' -ForegroundColor Yellow
$aux = ([Microsoft.Win32.Registry]::LocalMachine.
OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment")).
GetValue("Path",$False,
[Microsoft.Win32.RegistryValueOptions]::
DoNotExpandEnvironmentNames) -replace ';$'
$aux -split ";" | ForEach-Object {
[PSCustomObject]@{
check = 'HKLM:Path';
valid = myTest-Path -PathString "$_";
path = "$_"
}
}
Write-Host 'checking $env:Path duplicates' -ForegroundColor Yellow
$auxArr = $env:Path.split( ";", [System.StringSplitOptions]::RemoveEmptyEntries )
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) ) {
Write-Host $('{0,4} {1,4} "{2}"' -f $i, $j, $auxArr[$i]) -ForegroundColor Cyan
[PSCustomObject]@{
check = 'duplicate';
valid = "$i×$j";
path = $auxArr[$i]
}
}
} catch {
Write-Host "$i $j invalid folder name in `$env:Path" -ForegroundColor Red
Write-Host "$i`: [$($auxArr[$i])]" -ForegroundColor Red
Write-Host "$j`: [$($auxArr[$j])]" -ForegroundColor Red
}
}
}
Edit: script updated:
- handles possible empty items correctly (I met weird
;;
in$env:Path
), and - returns an array of
PSCustomObject
s for better (potential) consecutive manipulation.
Related videos on Youtube
Kareem
Updated on September 18, 2022Comments
-
Kareem over 1 year
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 myPath
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?
-
André about 7 yearsI 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.
-
BrianC about 7 yearsBy 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.
-
Kareem about 7 yearsThanks 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 about 7 yearsThanks @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!