Change shortcut links to new location

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/
Julio ArizagaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
Probably the simplest solution is to double the backslashes when they appear in patterns.

But for you, you're not really using a regular expression, you're just checking to see whether two strings are equal for the length of the shorter string, so you could do better with:


if($oldpath.substring(0,$oldsharepath.length) -eq $oldsharepath)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian ScholerCommented:
-match is the regular expression matching operator, so you must use regular expression syntax. In a regular expression, the backslash is the escape character.

You want to do a partial match here. The easiest way is to the -like operator instead, which uses a much more simple syntax:

if($oldpath -like "$oldsharepath*")
{
    # do stuff
}

Open in new window


To do this in a regular expression, a partial match at the beginning of the string would be written as:

^StringToMatch

Open in new window


The caret matches the start of the string, then you put the literal string. Problem in your case is that it's a variable, so you can't tell in advance what kinds of special regex characters might exist.

Luckily there's a programmatic way to escape any special characters so you know you match the literals:

[RegEx]::Escape($string)

Open in new window


So your match would then look like this:

if($oldpath -match "^$([RegEx]::Escape($oldsharepath))")
{ }

Open in new window


Or, broken out more:

$reg = "^" + [RegEx]::Escape($oldsharepath)
if($oldpath -match $reg)
{ }

Open in new window

Julio ArizagaAuthor Commented:
GREAT!! That worked. Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.