Change location to shortcut location Change location to shortcut location powershell powershell

Change location to shortcut location


You can make a shortcut that starts in whatever directory it's located in. All you have to do is modify the "Starts In" property of the shortcut, and blank it out. That's right. Set it to nothing.

Then, when you want to invoke the shortcut, navigate to the folder (directory) where it's located before invoking it. I use this technique for a shortcut that launches powershell but doesn't launch a script. I haven't tested it with a shortcut that launches a script.


Walter Mitty's helpful answer provides an effective solution, provided that the shortcut file is opened from either the Desktop or File Explorer.

This answer provides background information.


You must configure the desired working directory as part of the shortcut file, because the script you invoke knows nothing about the shortcut that invoked it.

Therefore, to configure a specific working directory (e.g., C:\Work\Project1), specify it in the shortcut file's Properties dialog in the Start in: field.

In case you want to update a shortcut file's (*.lnk) working directory programmatically, use the technique from this answer with the .WorkingDirectory property.

Note: In both cases, only an absolute path can be configured as the working directory: File Explorer only allows you to enter an absolute path anyway, and while the programmatic method allows you to assign a relative path, it is instantly resolved to an absolute one, relative to the shortcut's location.

To make the shortcut's own directory the working directory, you can blank out the Start in: field / .WorkingDirectory property, but note the limitations:

  • Only works when such a shortcut is either opened from either the Desktop or from File Explorer.

  • By contrast, opening it from the taskbar or (pre-Windows 10 only) the Start Menu, the working directory is $env:windir\System32 (typically, C:\Windows\System32).


If the shortcut targets an application (rather than a document), as in this case, that application invoked by the shortcut - including cmd.exe and powershell.exe - starts in the configured / implied working directory.

Caveat re cmd.exe: If the working directory is specified as as a UNC path, cmd.exe won't be able to change to that directory; as a workaround, use a path with a mapped drive instead (but, obviously, that drive must be mapped at the time the shortcut is opened).


You can use the TargetPath property of the shortcut (maybe do a get-childitem $psscriptroot where name is like scriptname and extension is like .lnk) with the set-location cmdlet

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-6