Changing target path of windows shortcuts


  1.    31 Oct 2007 #1
    Tim Guest

    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

  2.    31 Oct 2007 #2
    Shay Levi Guest

    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



    > 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

  3.    31 Oct 2007 #3
    Shay Levi Guest

    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



    > 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

    >> 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

  4.    31 Oct 2007 #4
    Tim Guest

    Re: Changing target path of windows shortcuts


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

    > 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

  5.    31 Oct 2007 #5
    Shay Levi Guest

    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



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

    >> 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

  6.    31 Oct 2007 #6
    Tim Guest

    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:

    > 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
    >

    > > On Oct 31, 9:34 am, Shay Levi <n...@xxxxxx> wrote:
    >
      My System SpecsSystem Spec

  7.    27 May 2008 #7
    Join Date : May 2008
    Posts : 5
    Vistax64 Business
    Local Time: 14:55


     

    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

  8.    28 May 2008 #8
    Shay Levi Guest

    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

    > 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

  9.    28 May 2008 #9
    Join Date : May 2008
    Posts : 5
    Vistax64 Business
    Local Time: 14:55


     

    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

  10.    29 May 2008 #10
    Shay Levi Guest

    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

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

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Changing target path of windows shortcuts

Similar Threads
Thread Forum
Shortcut Target Path Location
How to Change the Target Path Location of a Shortcut in Vista This will show you how to change the Target path location of a shortcut file or...
Tutorials
Changing the Shortcut's target path
I haven't check on other forums, I'm sorry if this has been posted before. I got a question regarding in changing a shortcut's target path. ...
Vista file management
Target Path for Windows Mail?
I have recently downloaded the latest version of Mozilla and am attempting to use the “get mail” add-on. Although I have managed to add the icon...
Browsers & Mail
Changing icons on desktop shortcuts
When I try to change the icons on some of the shortcuts I've placed on the desktop (by using the properties tab), some of them "take" and some...
Vista General
Changing WMP keyboard shortcuts
Hi, does anyone know how to change the keyboard shortcuts in Windows Media Player version 11? I'd like to change the shortcut for pausing to the...
Vista music pictures video
Changing the path statement
How can I change the path statement in Vista? I want to have it load the new path every time I boot up, not just a one-time change. Thanks,...
Vista General
Changing default import path in Windows Movie Maker
When importing digital video from camera I am trying to change the default store location to something other than C:\Myprofile\Videos. You can...
Vista General

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    © Designer Media Ltd
    All times are GMT -5. The time now is 13:55.
    .