Skip to content

Instantly share code, notes, and snippets.

@kareiva
Last active January 16, 2023 09:19
Show Gist options
  • Save kareiva/0a3a922d3408d10bd01b7d81ba48b481 to your computer and use it in GitHub Desktop.
Save kareiva/0a3a922d3408d10bd01b7d81ba48b481 to your computer and use it in GitHub Desktop.
LNK rescue (put icons on desktop)
$programs = @{
"Adobe Acrobat" = "Acrobat.exe"
"Firefox Private Browsing" = "private_browsing.exe"
"Firefox" = "firefox.exe"
"Google Chrome" = "chrome.exe"
"Microsoft Edge" = "msedge.exe"
"Parallels Client" = "APPServerClient.exe"
"Remote Desktop" = "msrdcw.exe"
"TeamViewer" = "TeamViewer.exe"
"Royal TS6" = "royalts.exe"
"Elgato StreamDeck" = "StreamDeck.exe"
"Visual Studio 2022" = "devenv.exe"
"Visual Studio Code" = "code.exe"
"Camtasia Studio" = "CamtasiaStudio.exe"
"Camtasia Recorder" = "CamtasiaRecorder.exe"
"Jabra Direct" = "jabra-direct.exe"
"7-Zip File Manager" = "7zFM.exe"
"Access" = "MSACCESS.EXE"
"Excel" = "EXCEL.EXE"
"OneDrive" = "onedrive.exe"
"OneNote" = "ONENOTE.EXE"
"Outlook" = "OUTLOOK.EXE"
"PowerPoint" = "POWERPNT.EXE"
"Project" = "WINPROJ.EXE"
"Publisher" = "MSPUB.EXE"
"Visio" = "VISIO.EXE"
"Word" = "WINWORD.exe"
"PowerShell 7 (x64)" = "pwsh.exe"
"SQL Server Management Studio" = "ssms.exe"
"Azure Data Studio" = "azuredatastudio.exe"
}
$LogFileName = "ShortcutRepairs.log";
$LogFilePath = "$env:temp\$LogFileName";
Function Log {
param($message);
$currenttime = Get-Date -format u;
$outputstring = "[" + $currenttime + "] " + $message;
$outputstring | Out-File $LogFilepath -Append;
}
Function LogAndConsole($message) {
Write-Host $message -ForegroundColor Green
Log $message
}
Function LogErrorAndConsole($message) {
Write-Host $message -ForegroundColor Red
Log $message
}
Function CopyAclFromOwningDir($path) {
$base_path = Split-Path -Path $path
$acl = Get-Acl $base_path
$group = New-Object System.Security.Principal.NTAccount("Builtin", "Administrators")
$acl.SetOwner($group)
Set-Acl $path $acl
}
# Validate elevated privileges
LogAndConsole "Starting LNK rescue"
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if (!($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) -Or ($id.Name -like "NT AUTHORITY\SYSTEM"))) {
LogErrorAndConsole "Not running from an elevated context"
throw "Please run this script from an elevated PowerShell as Admin or as System"
exit
}
# Check for shortcuts in Start Menu, if program is available and the shortcut isn't... Then recreate the shortcut
$success = 0
$failures = 0
LogAndConsole "Enumerating installed software under HKLM"
$programs.GetEnumerator() | ForEach-Object {
$reg_path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\$($_.Value)"
try {
$apppath = $null
$target = $null
try { $apppath = Get-ItemPropertyValue $reg_path -Name "Path" -ErrorAction SilentlyContinue } catch {}
if ($apppath -ne $null)
{
$target = $apppath + "\" + $_.Value
}
else
{
try { $target = Get-ItemPropertyValue $reg_path -Name "(default)" -ErrorAction SilentlyContinue } catch {}
}
if ($target -ne $null) {
if (-not (Test-Path -Path "C:\Users\Public\Desktop\$($_.Key).lnk")) {
LogAndConsole ("Shortcut for {0} not found in \Desktop\, creating it now." -f $_.Key)
$target = $target.Trim("`"")
$shortcut_path = "C:\Users\Public\Desktop\$($_.Key).lnk"
$description = $_.Key
$workingdirectory = (Get-ChildItem $target).DirectoryName
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($shortcut_path)
$Shortcut.TargetPath = $target
$Shortcut.Description = $description
$shortcut.WorkingDirectory = $workingdirectory
$Shortcut.Save()
Start-Sleep -Seconds 1 # Let the LNK file be backed to disk
LogAndConsole "Copying ACL from owning folder"
CopyAclFromOwningDir $shortcut_path
$success += 1
}
}
}
catch {
$failures += 1
LogErrorAndConsole "Exception: $_"
}
}
LogAndConsole "Finished with $failures failures and $success successes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment