Is there an easy way to ensure that the values in my PATH environment variable are valid?

John Savill

May 15, 2007

1 Min Read
ITPro Today logo in a gray background | ITPro Today

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

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like