Skip to content

Instantly share code, notes, and snippets.

@asgaut
Created April 14, 2020 12:43
Show Gist options
  • Select an option

  • Save asgaut/7d4289f80af1a3d225d30575143eb3d1 to your computer and use it in GitHub Desktop.

Select an option

Save asgaut/7d4289f80af1a3d225d30575143eb3d1 to your computer and use it in GitHub Desktop.
Check user and system path variables for duplicate entries
# Run with administrator privileges
$RegKey = ([Microsoft.Win32.Registry]::CurrentUser).OpenSubKey("Environment", $True)
$PathValue = $RegKey.GetValue("Path", $Null, "DoNotExpandEnvironmentNames")
$RegKey.Close()
$UserPathValues = $PathValue.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
Write-host "Current User path:`n " ($UserPathValues -join "`n ")
$RegKey = ([Microsoft.Win32.Registry]::LocalMachine).OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $True)
$PathValue = $RegKey.GetValue("Path", $Null, "DoNotExpandEnvironmentNames")
$RegKey.Close()
$SystemPathValues = $PathValue.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
Write-host "`nCurrent System path:`n " ($SystemPathValues -join "`n ")
$DuplicateFound = $false
$DedupUserPathValues = @()
ForEach ($Value in $UserPathValues)
{
if ($DedupUserPathValues -notcontains $Value)
{
$DedupUserPathValues += $Value
}
else
{
Write-Host -ForegroundColor Yellow "Duplicate User Path entry found:" $Value
$DuplicateFound = $true
}
}
$DedupSystemPathValues = @()
ForEach ($Value in $SystemPathValues)
{
if ($DedupSystemPathValues -notcontains $Value)
{
$DedupSystemPathValues += $Value
}
else
{
Write-Host -ForegroundColor Yellow "Duplicate System Path entry found:" $Value
$DuplicateFound = $true
}
}
ForEach ($Value in $DedupSystemPathValues)
{
if ($DedupUserPathValues -contains $Value)
{
Write-Host -ForegroundColor Yellow "In both User and System Paths:" $Value
$DuplicateFound = $true
}
}
Write-Host
if (!$DuplicateFound) {
Write-Host -ForegroundColor Green "No duplicates found"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment