Files
win-remote-toolkit/scripts/powershell/pin-rdp-to-taskbar.ps1
2026-04-24 17:44:02 +08:00

72 lines
2.1 KiB
PowerShell

[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = 'Continue'
Write-Host "[*] Pin RDP connection to taskbar"
# File paths
$rdpFile = "C:\Users\User\Desktop\Remote-Desktop.rdp"
$shortcutPath = "C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Remote Desktop.lnk"
# Check if RDP file exists
if (-not (Test-Path $rdpFile)) {
Write-Host "[!] RDP file not found: $rdpFile"
exit 1
}
Write-Host "[*] Creating Start Menu shortcut"
# Create WScript.Shell object
$WshShell = New-Object -comObject WScript.Shell
# Create shortcut
$Shortcut = $WshShell.CreateShortcut($shortcutPath)
$Shortcut.TargetPath = "mstsc.exe"
$Shortcut.Arguments = "`"$rdpFile`""
$Shortcut.WorkingDirectory = "C:\Windows\System32"
$Shortcut.IconLocation = "mstsc.exe,0"
$Shortcut.Description = "Remote Desktop Connection"
$Shortcut.WindowStyle = 1
$Shortcut.Save()
Write-Host " Shortcut created: $shortcutPath"
# Pin to taskbar method
Write-Host "[*] Attempting to pin to taskbar"
try {
# Method 1: Try using verb
$shell = New-Object -ComObject Shell.Application
$folder = $shell.Namespace((Split-Path $shortcutPath))
$item = $folder.ParseName((Split-Path $shortcutPath -Leaf))
$verbs = $item.Verbs()
$pinVerb = $verbs | Where-Object { $_.Name -match "Pin to taskbar|taskbar" }
if ($pinVerb) {
Write-Host " Found pin verb: $($pinVerb.Name)"
$pinVerb.DoIt()
Write-Host "[OK] Successfully pinned to taskbar"
} else {
Write-Host "[!] Pin to taskbar verb not found"
Write-Host "Manual steps:"
Write-Host "1. Open Start Menu"
Write-Host "2. Find 'Remote Desktop' shortcut"
Write-Host "3. Right-click > Pin to taskbar"
}
} catch {
Write-Host "[!] Auto-pin failed: $($_.Exception.Message)"
Write-Host "Please pin manually from Start Menu"
}
# Optionally remove desktop RDP file
Write-Host "[*] Cleaning up desktop RDP file"
try {
Remove-Item $rdpFile -Force
Write-Host " Desktop RDP file removed"
} catch {
Write-Host " Desktop RDP file kept"
}
Write-Host ""
Write-Host "[OK] Setup complete! Check Start Menu for Remote Desktop shortcut"