Skip to content

Instantly share code, notes, and snippets.

@obsti8383
Created March 11, 2023 12:38
Show Gist options
  • Save obsti8383/80e2a44ab8fa74a9e03149caf342cd09 to your computer and use it in GitHub Desktop.
Save obsti8383/80e2a44ab8fa74a9e03149caf342cd09 to your computer and use it in GitHub Desktop.
##
## deletes all link files on Desktop and Common Desktop Directories, that link to Program Files.
##
# Get the desktop folder path
$desktopPath = [Environment]::GetFolderPath("Desktop")
$desktopPathPublic = [Environment]::GetFolderPath("CommonDesktopDirectory")
$ErrorActionPreference = "Stop"
# Get a list of all the shortcut files in the desktop folder
$shortcutFiles = Get-ChildItem $desktopPath -Filter *.lnk
$shortcutFiles += Get-ChildItem $desktopPathPublic -Filter *.lnk
$Shell = New-Object -ComObject WScript.Shell
# Loop through each shortcut file and check if it targets an installed program
foreach ($shortcutFile in $shortcutFiles) {
Write-Host "Checking $shortcutFile"
$target = $Shell.CreateShortcut($shortcutFile.FullName).TargetPath
#$target
if ($target -match "^([A-Z]:\\)?(Program Files|Program Files \(x86\))\\") {
Write-Host "Removing $shortcutFile"
Remove-Item $shortcutFile.FullName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment