Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Changing target path of windows shortcuts

Reply
 
Old 10-31-2007   #1 (permalink)
Tim


 
 

Changing target path of windows shortcuts

I have a user who created a lot of windows shortcuts and now the
target path has changed so we need to modify these with a script. And
its not as simple as replacing one server name with another. In fact,
all the shortcuts in question point to files on the user's C: drive.
Because of a change in an application, those files now have to be
stored in a different directory on the C: drive.

If it matters, old path is "c:\documents and settings\localservice
\application data". New path is "c:\documents and settings\username
\local settings\application data\."

Is there an easy way to do a recursive search for shortcut files and
modify the target path for each one if it has a specific string in
it?

Thanks,

Tim


My System SpecsSystem Spec
Old 10-31-2007   #2 (permalink)
Shay Levi


 
 

Re: Changing target path of windows shortcuts

There is no specific command for it. You'll have to use wscript.shell com
object.
You can't *get* the lnk file target path, the trick is to *create* it again:

1. Create new shortcut for each shortcut you find
2. Get its targetpath for comparison
4. If your criteria matches, set the new target path
5. Save the lnk file.

$shell = new-object -com wscript.shell

get-childitem -filter *.lnk -recurse | foreach {
$lnk = $shell.CreateShortcut($_.fullname)
$oldpath= $lnk.TargetPath
if($oldpath -eq $yourCriteria"){
$lnk.TargetPath = "new_path"
$lnk.Save()
}
}



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> I have a user who created a lot of windows shortcuts and now the
> target path has changed so we need to modify these with a script. And
> its not as simple as replacing one server name with another. In fact,
> all the shortcuts in question point to files on the user's C: drive.
> Because of a change in an application, those files now have to be
> stored in a different directory on the C: drive.
>
> If it matters, old path is "c:\documents and settings\localservice
> \application data". New path is "c:\documents and settings\username
> \local settings\application data\."
>
> Is there an easy way to do a recursive search for shortcut files and
> modify the target path for each one if it has a specific string in it?
>
> Thanks,
>
> Tim
>

My System SpecsSystem Spec
Old 10-31-2007   #3 (permalink)
Shay Levi


 
 

Re: Changing target path of windows shortcuts

I missed the get-childitem's -path parameter
it should be:

get-childitem <path> -filter *.lnk -recurse ...


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> There is no specific command for it. You'll have to use wscript.shell
> com
> object.
> You can't *get* the lnk file target path, the trick is to *create* it
> again:
> 1. Create new shortcut for each shortcut you find
> 2. Get its targetpath for comparison
> 4. If your criteria matches, set the new target path
> 5. Save the lnk file.
> $shell = new-object -com wscript.shell
>
> get-childitem -filter *.lnk -recurse | foreach {
> $lnk = $shell.CreateShortcut($_.fullname)
> $oldpath= $lnk.TargetPath
> if($oldpath -eq $yourCriteria"){
> $lnk.TargetPath = "new_path"
> $lnk.Save()
> }
> }
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
Quote:

>> I have a user who created a lot of windows shortcuts and now the
>> target path has changed so we need to modify these with a script.
>> And its not as simple as replacing one server name with another. In
>> fact, all the shortcuts in question point to files on the user's C:
>> drive. Because of a change in an application, those files now have to
>> be stored in a different directory on the C: drive.
>>
>> If it matters, old path is "c:\documents and settings\localservice
>> \application data". New path is "c:\documents and settings\username
>> \local settings\application data\."
>>
>> Is there an easy way to do a recursive search for shortcut files and
>> modify the target path for each one if it has a specific string in
>> it?
>>
>> Thanks,
>>
>> Tim
>>

My System SpecsSystem Spec
Old 10-31-2007   #4 (permalink)
Tim


 
 

Re: Changing target path of windows shortcuts

On Oct 31, 9:34 am, Shay Levi <n...@xxxxxx> wrote:
Quote:

> There is no specific command for it. You'll have to use wscript.shell com
> object.
> You can't *get* the lnk file target path, the trick is to *create* it again:
>
> 1. Create new shortcut for each shortcut you find
> 2. Get its targetpath for comparison
> 4. If your criteria matches, set the new target path
> 5. Save the lnk file.
>
> $shell = new-object -com wscript.shell
>
> get-childitem -filter *.lnk -recurse | foreach {
> $lnk = $shell.CreateShortcut($_.fullname)
> $oldpath= $lnk.TargetPath
> if($oldpath -eq $yourCriteria"){
> $lnk.TargetPath = "new_path"
> $lnk.Save()
> }
>
> }
>
> -----
> Shay Levi
> $cript Fanatichttp://scriptolog.blogspot.com
>
Thanks Shay for the quick response!

You have me on the right path. I made a couple of changes to your
script and have listed it below. I don't want to change the entire
target path of the shortcut so I need to be able to replace a block of
text with another. Here is what I'm trying:

$psshell = new-object -com wscript.shell
$oldtarget = "C:\Documents and Settings\LocalService\Application Data
\ColligoOfflineClient\Storage3\Files\Sites"
$newtarget = "C:\Documents and Settings\username\Local Settings
\Application Data\ColligoOfflineClient\storage5\Files\Sites"

get-childitem c:\testshortcut -filter *.lnk -recurse | foreach {
$lnk = $psshell.CreateShortcut($_.fullname)
$oldpath = $lnk.TargetPath
if($oldpath -match "ColligoOfflineClient"){
$newpath = $oldpath -replace($oldtarget,$newtarget)
$lnk.TargetPath = $newpath
$lnk.Save()
}
}

When I run this, I'm getting this error:

Invalid regular expression pattern: C:\Documents and Settings
\LocalService\Application Data\ColligoOfflineClient\Storage3\Files
\Sites.
At H:\scripts\shortcut.ps1:10 char:47
+ $newpath = $oldpath -replace($oldtarget, <<<< $newtarget)

I'm sure this is something simple? What am I missing?

My System SpecsSystem Spec
Old 10-31-2007   #5 (permalink)
Shay Levi


 
 

Re: Changing target path of windows shortcuts


This is because the backslash ("\") char is a regular expression meta character.
To avoid such issues you can use the [regex] static methods: escape & unescape

$psshell = new-object -com wscript.shell
$oldtarget = [regex]::escape("C:\Documents and Settings\LocalService\Application
Data\ColligoOfflineClient\Storage3\Files\Sites")
$newtarget = [regex]::escape("C:\Documents and Settings\username\Local Settings\Application
Data\ColligoOfflineClient\storage5\Files\Sites")

get-childitem c:\testshortcut -filter *.lnk -recurse | foreach {
$lnk = $psshell.CreateShortcut($_.fullname)
$oldpath = [regex]::escape($lnk.TargetPath)
if($oldpath -match "ColligoOfflineClient"){
$newpath = $oldpath -replace($oldtarget,$newtarget)
$lnk.TargetPath = [regex]::unescape($newpath)
$lnk.Save()
}
}


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> On Oct 31, 9:34 am, Shay Levi <n...@xxxxxx> wrote:
>
Quote:

>> There is no specific command for it. You'll have to use wscript.shell
>> com
>> object.
>> You can't *get* the lnk file target path, the trick is to *create* it
>> again:
>> 1. Create new shortcut for each shortcut you find
>> 2. Get its targetpath for comparison
>> 4. If your criteria matches, set the new target path
>> 5. Save the lnk file.
>> $shell = new-object -com wscript.shell
>>
>> get-childitem -filter *.lnk -recurse | foreach {
>> $lnk = $shell.CreateShortcut($_.fullname)
>> $oldpath= $lnk.TargetPath
>> if($oldpath -eq $yourCriteria"){
>> $lnk.TargetPath = "new_path"
>> $lnk.Save()
>> }
>> }
>>
>> -----
>> Shay Levi
>> $cript Fanatichttp://scriptolog.blogspot.com
> Thanks Shay for the quick response!
>
> You have me on the right path. I made a couple of changes to your
> script and have listed it below. I don't want to change the entire
> target path of the shortcut so I need to be able to replace a block of
> text with another. Here is what I'm trying:
>
> $psshell = new-object -com wscript.shell
> $oldtarget = "C:\Documents and Settings\LocalService\Application Data
> \ColligoOfflineClient\Storage3\Files\Sites"
> $newtarget = "C:\Documents and Settings\username\Local Settings
> \Application Data\ColligoOfflineClient\storage5\Files\Sites"
> get-childitem c:\testshortcut -filter *.lnk -recurse | foreach {
> $lnk = $psshell.CreateShortcut($_.fullname)
> $oldpath = $lnk.TargetPath
> if($oldpath -match "ColligoOfflineClient"){
> $newpath = $oldpath -replace($oldtarget,$newtarget)
> $lnk.TargetPath = $newpath
> $lnk.Save()
> }
> }
> When I run this, I'm getting this error:
>
> Invalid regular expression pattern: C:\Documents and Settings
> \LocalService\Application Data\ColligoOfflineClient\Storage3\Files
> \Sites.
> At H:\scripts\shortcut.ps1:10 char:47
> + $newpath = $oldpath -replace($oldtarget, <<<< $newtarget)
> I'm sure this is something simple? What am I missing?
>

My System SpecsSystem Spec
Old 10-31-2007   #6 (permalink)
Tim


 
 

Re: Changing target path of windows shortcuts

Thanks again. That didn't work out like I had planned, but then I
discovered that there is a difference between -replace and .replace.
Here is the working script and it seems to be working well:

$psshell = new-object -com wscript.shell

get-childitem c:\testshortcut -filter *.lnk -recurse | foreach {
$lnk = $psshell.CreateShortcut($_.fullname)
$oldpath = $lnk.TargetPath
if($oldpath -match "ColligoOfflineClient"){
$newtarget = $oldpath.replace("C:\Documents and Settings
\LocalService\Application Data\ColligoOfflineClient\Storage3\Files
\Sites","C:\Documents and Settings\username\Local Settings\Application
Data\ColligoOfflineClient\storage5\Files\Sites")
$lnk.TargetPath = $newtarget
$lnk.Save()
}
}

I'm amazed on a daily basis by the power (and complexity) of
powershell.

On Oct 31, 2:03 pm, Shay Levi <n...@xxxxxx> wrote:
Quote:

> This is because the backslash ("\") char is a regular expression meta character.
> To avoid such issues you can use the [regex] static methods: escape & unescape
>
> $psshell = new-object -com wscript.shell
> $oldtarget = [regex]::escape("C:\Documents and Settings\LocalService\Application
> Data\ColligoOfflineClient\Storage3\Files\Sites")
> $newtarget = [regex]::escape("C:\Documents and Settings\username\Local Settings\Application
> Data\ColligoOfflineClient\storage5\Files\Sites")
>
> get-childitem c:\testshortcut -filter *.lnk -recurse | foreach {
> $lnk = $psshell.CreateShortcut($_.fullname)
> $oldpath = [regex]::escape($lnk.TargetPath)
> if($oldpath -match "ColligoOfflineClient"){
> $newpath = $oldpath -replace($oldtarget,$newtarget)
> $lnk.TargetPath = [regex]::unescape($newpath)
> $lnk.Save()
> }
>
> }
>
> -----
> Shay Levi
> $cript Fanatichttp://scriptolog.blogspot.com
>
Quote:

> > On Oct 31, 9:34 am, Shay Levi <n...@xxxxxx> wrote:
>
My System SpecsSystem Spec
Old 05-27-2008   #7 (permalink)


Vistax64 Business
 
 

Re: Changing target path of windows shortcuts

I need to do something similar and need some assistance. A user has a lot of shortcuts on his desktop that point to c:\data\ and a bunch of subflders. I need to replace just the c:\data portion to \\server\data

From what I understand the final script should do just that but I do not know how to run this script in powershell. I installed powershell 1.0 for Vista x64 Edition (KB928439) but have no idea how to run the script.

Thanks for any help here.

Vista x64 Business machine

Steve Barak.
My System SpecsSystem Spec
Old 05-28-2008   #8 (permalink)
Shay Levi


 
 

Re: Changing target path of windows shortcuts



Hi Steve,

Try this version:


dir "$env:USERPROFILE\desktop" -filter *.lnk -recurse | foreach {
$lnk = $shell.createShortcut($_.fullname)
$oldPath= $lnk.targetPath

if($oldpath -match "^c:\\data"){
$lnk.targetPath = $oldPath -replace "c:\\data","\\server"
$lnk.save()
}
}




---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> I need to do something similar and need some assistance. A user has a
> lot of shortcuts on his desktop that point to c:\data\ and a bunch of
> subflders. I need to replace just the c:\data portion to \\server\data
>
> From what I understand the final script should do just that but I do
> not know how to run this script in powershell. I installed powershell
> 1.0 for Vista x64 Edition (KB928439) but have no idea how to run the
> script.
>
> Thanks for any help here.
>
> Vista x64 Business machine
>
> Steve Barak.
>

My System SpecsSystem Spec
Old 05-28-2008   #9 (permalink)


Vistax64 Business
 
 

Re: Changing target path of windows shortcuts

How do I actually run the script? Do I have to save it as a .vbs or something?
My System SpecsSystem Spec
Old 05-29-2008   #10 (permalink)
Shay Levi


 
 

Re: Changing target path of windows shortcuts



Put the below in file and save it as a .ps1 file (powershell script file).



####### shortCut.ps1 ######

dir "$env:USERPROFILE\desktop" -filter *.lnk -recurse | foreach {
$lnk = $shell.createShortcut($_.fullname)
$oldPath= $lnk.targetPath

if($oldpath -match "^c:\\data"){
$lnk.targetPath = $oldPath -replace "c:\\data","\\server"
$lnk.save()
}
}


######################


For example, if you saved it to the d:\scripts folder then open PowerShell
console
, type the full path to the script and press enter to excute it:

D:\Scripts\shortCut.ps1





---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> How do I actually run the script? Do I have to save it as a .vbs or
> something?
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Changing the Shortcut's target path Vista file management
Target Path for Windows Mail? Browsers & Mail
Shortcut Target Path Location Tutorials
Changing WMP keyboard shortcuts Vista music pictures video
Changing default import path in Windows Movie Maker Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46