Skip to content

Instantly share code, notes, and snippets.

@alexverboon
Created November 2, 2019 12:32
Show Gist options
  • Save alexverboon/2337f061f28a6a273140a53fd0bef838 to your computer and use it in GitHub Desktop.
Save alexverboon/2337f061f28a6a273140a53fd0bef838 to your computer and use it in GitHub Desktop.
Test-EnvPathHealth
function Test-EnvPathHealth
{
<#
.Synopsis
Test-EnvPathHealth
.DESCRIPTION
Test-EnvPathHealth tests if the paths defined within the %PATH% environment variable
exist.
.PARAMETER Writable
The writable switch instructs Test-EnvPathHealth to test whether the user when running
PowerShell with standard user rights has write access to the folder.
The cmdlet will attempt to create a test file, when successfull it leaves the test file
in the target folder.
Example: Test-EnvPathHealth_559233ce-723b-43a0-8c18-f9b83c703989.txt
Note, the cmdlet will NOT remove this test file when completed.
.EXAMPLE
Test-EnvPathHealth
This command checks if each folder specified in the path variabe exists.
PathExists Path Writable
---------- ---- --------
True C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\
True C:\WINDOWS\system32
True C:\WINDOWS
True C:\WINDOWS\System32\Wbem
True C:\WINDOWS\System32\WindowsPowerShell\v1.0\
True C:\WINDOWS\System32\OpenSSH\
True C:\Program Files\dotnet\
True C:\Program Files\Git\cmd
True C:\Program Files\PuTTY\
True c:\Program Files\PowerShell\7-preview\preview
False C:\Program Files\PowerShell\6\
True C:\Users\alve\AppData\Local\Microsoft\WindowsApps
True C:\Users\alve\AppData\Local\Programs\Microsoft VS Code\bin
.EXAMPLE
Test-EnvPathHealth -Writable
This command checks if each folder specified in the path variabe exists and then tries to write
a test file Test-EnvPathHealth_<RANDOMGUID>.txt into the folder to check if a user with standard
user rights has write access to the folder.
PathExists Path Writable
---------- ---- --------
True C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\ False
True C:\WINDOWS\system32 False
True C:\WINDOWS False
True C:\WINDOWS\System32\Wbem False
True C:\WINDOWS\System32\WindowsPowerShell\v1.0\ False
True C:\WINDOWS\System32\OpenSSH\ False
True C:\Program Files\dotnet\ False
True C:\Program Files\Git\cmd False
True C:\Program Files\PuTTY\ False
True c:\Program Files\PowerShell\7-preview\preview False
False C:\Program Files\PowerShell\6\ False
True C:\Users\alve\AppData\Local\Microsoft\WindowsApps True
True C:\Users\alve\AppData\Local\Programs\Microsoft VS Code\bin True
.NOTES
Author: Alex Verboon
Version: 1.0.0
Date: 02.11.2019
Website: https://verboon.info
#>
[CmdletBinding()]
Param
(
[switch]$Writable
)
Begin
{
$EnvPathHealth = @()
}
Process
{
ForEach ($xPath in ($env:path).Split(";"))
{
If (-not [string]::IsNullOrEmpty($xPath))
{
If (Test-Path -Path $xPath)
{
$FolderExists = $true
}
Else
{
$FolderExists = $false
}
If ($Writable)
{
if ( ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "You are running this as local administrator. Run it again with standard users rights" ; break
}
TrY{
$randomfilename = (New-Guid).Guid
$logdate = "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
Add-Content -Path "$xPath\Test-EnvPathHealth_$randomfilename.txt" -Value "$logdate Generated by Test-EnvPathHealth User:$ENV:USERNAME" -ErrorAction Stop
$FolderWritable = $true
}
Catch
{
$FolderWritable = $false
}
}
$result = [PSCustomObject] [ordered] @{
PathExists = $FolderExists
Path = $xPath
Writable = $FolderWritable
}
$EnvPathHealth = $EnvPathHealth + $result
}
}
}
End
{
$EnvPathHealth
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment