Powershell Script To Mass Change Shortcut Path

I have been looking at getting a script together for a while to mass change some shortcuts we have on our file server to point to a different location. After some research, and some playing around I found a really easy way to do this in Powershell:


# Call wscript com object
$shell = new-object -com wscript.shell

# Recurse through directories for .lnk files
dir "I:\" -filter *.lnk -recurse | foreach {
$lnk = $shell.createShortcut($_.fullname)
$oldPath= $lnk.targetPath

# If match text, perform operation
if($oldpath -match "\\serverold\share1")
{
write-host "Match: " + $_.fullname
remove-item $_.fullname
$lnknew = $shell.createShortcut($_.fullname)
$lnknew.targetPath = "`"\\newserver\share1`""
$lnknew.IconLocation = "%SystemRoot%\system32\SHELL32.dll,4"
$lnknew.Save()
}
}
Write-Host "End..."

2 Comments

  • Andy commented on August 9, 2012

    Hello,

    only Problem is…. Link before \\server1\C$\temp -> Link after \\server2\
    so not really the perfect solution.

  • Edgar commented on December 15, 2012

    Thank you, it has put me in the right track!

Leave a Reply

Your email address will not be published. Required fields are marked *