Public/Remove-specDesktopShortcut.ps1
function Remove-specDesktopShortcut { <# .SYNOPSIS Deletes a desktop shortcut. .DESCRIPTION This function deletes a desktop shortcut based on the specified properties. .PARAMETER ShortcutName The name of the shortcut to be deleted (excluding the .lnk extension). .EXAMPLE Remove-specDesktopShortcut -ShortcutName "MyApp" Deletes the desktop shortcut named "MyApp". .EXAMPLE $shortcutParams = [pscustomobject]@{ ShortcutName = 'Tonoref' AllUsers = $true } $shortcutparams | Remove-specDesktopShortcut Using pipeline to delete a shortcut from All Users desktop. .NOTES File: Remove-specDesktopShortcut.ps1 Author: owen.heaume 1.0 - Initial function creation #> [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string[]]$ShortcutName, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [switch]$AllUsers ) begin { } Process { foreach ($name in $ShortcutName) { # Determine the desktop path if ($AllUsers) { # Use the 'CommonDesktopDirectory' for All Users $desktopPath = [Environment]::GetFolderPath("CommonDesktopDirectory") } else { # Current user's desktop $desktopPath = [Environment]::GetFolderPath("Desktop") } $shortcutPath = Join-Path $desktopPath "$name.lnk" if (Test-Path $shortcutPath) { try { Remove-Item $shortcutPath -Force Write-Host "Successfully removed shortcut: $shortcutPath" -ForegroundColor DarkGreen } catch { Write-Warning $_.Exception.Message } } else { Write-Host "Shortcut not found: $shortcutPath" -ForegroundColor DarkYellow } } } } |