I would like to run the Test-Path, or something similar the completes my purpose to find the invalid paths in my path variable.
The main thing I have done is search for
test path system variable for invalid entries
This did not find anything.
This example is just to show I have tried something, but I don't really know what the best command it.
Test-Path -Path %Path% -PathType Any
Update
These scripts enabled my to find a couple bad paths and fix them
@Mathias
Here is @Mathias suggestion
PS C:\Program Files\RStudio\bin\quarto\bin> $env:Path -split ';'|?{-not($_|Test-Path)}
C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\
C:\RBuildTools\4.0usr\bin\
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:1 char:32
$env:Path -split ';'|?{-not($_|Test-Path)}
~~~~~~~~~
CategoryInfo : InvalidData: (:PSObject) [Test-Path], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Test
PathCommand
@panomosh
@panomosh suggestion
PS C:\Program Files\Quarto\bin> foreach($path in ((gci -path env:\path | select -exp Value).split(";"))){ if(Test-Path
path){ write-host "True - $path" -ForegroundColor Green } else { write-host "False - $path" -ForegroundColor Red }}
True - C:\Program Files (x86)\Common Files\Oracle\Java\javapath
True - C:\Python27\
True - C:\Python27\Scripts
True - C:\Oracle\app\client\product\12.2.0\client_1
True - C:\Oracle\app\client\product\12.2.0\client_1\bin
True - C:\Program Files\Microsoft MPI\Bin\
True - C:\Windows\system32
True - C:\Windows
True - C:\Windows\System32\Wbem
True - C:\Windows\System32\WindowsPowerShell\v1.0\
True - C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\
True - C:\Program Files\Microsoft SQL Server\130\DTS\Binn\
True - C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\
True - C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\
True - C:\Program Files\Microsoft SQL Server\130\Tools\Binn\
True - C:\Program Files\dotnet\
True - C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\
True - C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\
True - C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\
True - C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\
True - C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\
True - C:\Program Files (x86)\Wolfram Research\WolframScript\
True - C:\Program Files\Git\cmd
True - C:\Program Files\nodejs\
True - C:\Program Files (x86)\dotnet\
True - C:\Program Files\Wolfram Research\WolframScript\
True - C:\Program Files\Azure Data Studio\bin
True - C:\Program Files\Microsoft VS Code\bin
True - C:\Users\hnelson3\AppData\Local\Programs\Azure Data Studio\bin
True - C:\Program Files\Azure Data Studio\bin
True - C:\Users\hnelson3\AppData\Local\Programs\Microsoft VS Code Insiders\bin
True - C:\Users\hnelson3\AppData\Roaming\TinyTeX\bin\win32
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:1 char:88
... v:\path | select -exp Value).split(";"))){ if(Test-Path $path){ write ...
~~~~~
CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Test
PathCommand
CodePudding user response:
Heres a way to do it that will output each path on a new line, it will start with "false" and will be Red if the path is invalid, and will say "true" and be green if successful
foreach($path in ((gci -path env:\path | select -exp Value).split(";"))){ if(Test-Path $path){ write-host "True - $path" -ForegroundColor Green } else { write-host "False - $path" -ForegroundColor Red }}
CodePudding user response:
Building on Mathias R. Jessen's great solution in a comment:
# Output those entries that refer to nonexistent paths.
$env:PATH -split [IO.Path]::PathSeparator -ne '' |
Where-Object { -not (Test-Path $_) }
Using the all uppercase form
PATH
of the variable name and[IO.Path]::PathSeparator
as the separator to-split
by makes the command cross-platform:On Unix-like platforms environment variable names are case-sensitive, so using
$env:PATH
(all-upercase) is required; by contrast, Windows is not case-sensitive, so$env:PATH
works there too, even though the actual case of the name isPath
.On Unix-like platforms,
:
separates the entries in$env:PATH
, whereas it is;
on Windows -[IO.Path]::PathSeparator
returns the platform-appropriate character.
-ne ''
filters out any empty tokens resulting from the-split
operation, which could result from directly adjacent separators in the variable value (e.g.,;;
)- Note: With a an array as the LHS, such as
-split
, PowerShell comparison operators such as-eq
act as filters and return an array of matching items rather than a Boolean - see about_Comparison_Operators.
- Note: With a an array as the LHS, such as
The
Where-Object
call filters the input directory paths down to those that do not exist, and outputs them (which prints to the display by default).- Note that, strictly speaking,
Test-Path
's first positional parameter is-Path
, which interprets its argument as a wildcard expression. - For full robustness,
Test-Path -LiteralPath $_
is needed, to rule out inadvertent interpretation of literal paths that happen to contain[
as wildcards - though with entries in$env:PATH
that seems unlikely.
- Note that, strictly speaking,