Functions/Get-DeadLinks.ps1


function Get-DeadLinks {
    [CmdletBinding()]
    param (
        [Parameter()] [string] $Folder = ".",
        [Parameter()] [switch] $Remove
    )

    $shell = New-Object -ComObject WScript.Shell

    $Links = Get-ChildItem -Path $Folder -Recurse -Force -Filter "*.lnk" -ea 0
    # $Links

    foreach ($l in $Links) {

        $LinkProperties = $shell.CreateShortcut($l.FullName)
        if (!(Test-Path $LinkProperties.TargetPath)) {
            Write-Output "Link broken: `"$($LinkProperties.FullName)`" => `"$($LinkProperties.TargetPath)`""
            if ($Remove) {
                Remove-Item $LinkProperties.FullName -Force
                Write-Output "Removed link $($LinkProperties.FullName)"
            }
        }
    }

}