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 "Delete desktop Shortcut: Successfully removed shortcut: $shortcutPath" -ForegroundColor DarkGreen
                } catch {
                    Write-Warning $_.Exception.Message
                }
            } else {
                Write-Host "Delete desktop Shortcut: Shortcut does not exist: $shortcutPath" -ForegroundColor DarkYellow
            }
        }
    }
}