We help IT Professionals succeed at work.
Get Started

Change shortcut links to new location

Julio Arizaga
on
81 Views
Last Modified: 2015-07-31
Hello All,

I'm currently trying to get the following script to work, but need to adjust it to match anything with  "\\server1\IT". Using -eq works perfectly for folders, however when using -match I get the following error.

parsing "\\server1\IT" - Unrecognized escape sequence \I

Script should change files and folders location. Ex  "\\server1\IT\Document.docx" to "\\newserver\IT\Document.docx"



# Call wscript com object
$shell = new-object -com wscript.shell
$oldsharepath = "\\server1\IT"

# Recurse through directories for .lnk files
dir 'D:\Test Server1' -filter *.lnk -recurse | foreach {
$lnk = $shell.createShortcut($_.fullname)
$oldPath= $lnk.targetPath
# If match text, perform operation
if($oldpath -match $oldsharepath)
{
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..."
Code from: http://www.squiggle.org/2010/02/powershell-script-to-mass-change-shortcut-path/
Comment
Watch Question
Owner
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE