0

My shortcuts often get broken because I move stuff around and unfortunately, the Windows link tracker can't keep up. Is there a way to programmatically (with Powershell) read and edit the properties of the shortcut? I would like to run a script that searches the whole hard drive (or wherever I specify) for a file that matches the target name and then update the shortcut with that new location assuming it is the right file.

Roman
  • 344
  • 1
  • 6
  • 19
  • There is a couple of components to this but yes it is possible. Making the shorcut comes from http://stackoverflow.com/questions/9701840/how-to-create-a-shortcut-using-powershell. You can use that logic to help edit them as well. Try something and if you get stuck come back and update your question. – Matt Jan 29 '16 at 20:10

1 Answers1

-1

here is a function for creating shortcuts. you could research how it works to use it in your situation

https://github.com/gangstanthony/PowerShell/blob/master/Create-Shortcut.ps1

# Create-Shortcut
# 
# Create-Shortcut -Source C:\temp\test.txt -DestinationLnk C:\temp\test.txt.lnk
# 
# Arguments
# Description
# FullName
# Hotkey
# IconLocation = '%SystemRoot%\system32\SHELL32.dll,16' # printer
# RelativePath
# TargetPath
# WindowStyle
# WorkingDirectory

function Create-Shortcut {
    param (
        [string]$Source,
        [string]$DestinationLnk,
        [string]$Arguments
    )

    BEGIN {
        $WshShell = New-Object -ComObject WScript.Shell
    }

    PROCESS {
        if (!$Source) {Throw 'No Source'}
        if (!$DestinationLnk) {Throw 'No DestinationLnk'}

        $Shortcut = $WshShell.CreateShortcut($DestinationLnk)
        $Shortcut.TargetPath = $Source
        if ($Arguments) {
            $Shortcut.Arguments = $Arguments
        }
        $Shortcut.Save()
    }

    END {
        function Release-Ref ($ref) {
            ([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
            [System.GC]::Collect()
            [System.GC]::WaitForPendingFinalizers()
        }
        $Shortcut, $WshShell | % {$null = Release-Ref $_}
    }
}
Anthony Stringer
  • 1,981
  • 1
  • 10
  • 15