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.doc
x" to "\\newserver\IT\Document.d
ocx"
# 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($_.f
ullname)
$oldPath= $lnk.targetPath
# If match text, perform operation
if($oldpath -match $oldsharepath)
{
write-host "Match: " + $_.fullname
remove-item $_.fullname
$lnknew = $shell.createShortcut($_.f
ullname)
$lnknew.targetPath = "`"\\newserver\share1`""
$lnknew.IconLocation = "%SystemRoot%\system32\SHE
LL32.dll,4
"
$lnknew.Save()
}
}
Write-Host "End..."
Code from:
http://www.squiggle.org/2010/02/powershell-script-to-mass-change-shortcut-path/
You want to do a partial match here. The easiest way is to the -like operator instead, which uses a much more simple syntax:
Open in new window
To do this in a regular expression, a partial match at the beginning of the string would be written as:
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:
Open in new window
So your match would then look like this:
Open in new window
Or, broken out more:
Open in new window