Created
March 4, 2025 23:52
-
-
Save lucasmodrich/e9579f0d9fd23d6d2b32d6f8cd70edd8 to your computer and use it in GitHub Desktop.
Powershell Script to Check if Windows Environment Paths ($env:PATH) are Valid and Exist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
MIT License | |
Copyright (c) 2025 Lucas Modrich | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
#> | |
# Split the PATH environment variable into individual paths | |
$paths = $env:PATH -split ';' | |
# Initialize an array to store invalid paths | |
$invalidPaths = @() | |
# Iterate through each path in the PATH environment variable | |
foreach ($path in $paths) { | |
try { | |
# Check if the path is valid | |
$isValid = Test-Path -Path $path -IsValid | |
# Check if the path exists and is a directory | |
$exists = Test-Path -Path $path -PathType Container | |
# If the path is not valid or does not exist, add it to the invalidPaths array | |
if (-not $isValid -or -not $exists) { | |
$invalidPaths += [PSCustomObject]@{ | |
Path = $path | |
IsValid = $isValid | |
Exists = $exists | |
} | |
} | |
} catch { | |
# Output an error message if there is an issue checking the path | |
Write-Host "Error checking path: $path" | |
} | |
} | |
# Check if there are any invalid paths | |
if ($invalidPaths.Count -gt 0) { | |
# Output a message indicating there are invalid paths and display them in a table | |
Write-Host "The following paths in your PATH environment variable are invalid or do not exist:" -ForegroundColor Red | |
$invalidPaths | Format-Table -AutoSize | |
} else { | |
# Output a message indicating all paths are valid | |
Write-Host "All paths in your PATH environment variable are valid and exist." -ForegroundColor Green | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment