Is there an easy way to ensure that the values in my PATH environment variable are valid?
May 15, 2007
A. PowerShell provides an easy solution through its test-path cmdlet. You can pass each entry from the PATH variable to the test-path cmdlet, as follows:
$env:path.split(";") | test-path
True
True
True
True
True
True
True
In this example, each entry is valid in the path. If you want to also list the path part, you can obtain two lines. The only difference is that you save an array of the components, then test each array element.
$envpart = $env:path.split(";")
foreach ($envbit in $envpart) {$envbit; test-path $envbit}
C:WindowsSystem32WindowsPowerShellv1.0
True
C:Windowssystem32
True
C:Windows
True
C:WindowsSystem32Wbem
True
C:Program FilesDiskeeper CorporationDiskeeper
True
C:WindowsSystem32WindowsPowerShellv1.0
True
C:Program FilesWindows Imaging
True
About the Author
You May Also Like