4

I am launching a script with a powershell shortcut (C:\Windows....\powershell.exe -file 'D:\powershell\script.ps1').

Is there a way to make script change its current location to location of said shortcut?

Example: Script itself is in D:\powershell\ and the shortcut is in C:\Work\Project1. I need the script to cd to "C:\Work\Project1\".

Thanks

Jlisk
  • 153
  • 1
  • 1
  • 8

3 Answers3

3

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
  • 18,205
  • 2
  • 28
  • 58
  • I'm a bit late (sorry), but I'll enter the discussion here: I have a script located on our NAS It pulls data from somewhere, processes them and exports an excel file. I created a shortcut file to run it for our non IT people, so they need to only copy the shortcut. I want the resulting file to be saved in the same location as the shortcut they copied - if the person puts the shortcut in D:\Work\Project1, then I want the resulting excel file to be saved there too. – Jlisk Mar 07 '19 at 05:41
  • My answer is relevant to you, but it requres some explanaton. If you blank out the Starts In property of the shortcut, the process nvoked by it will inherit the current folder or directory. If the user opens the containng folder before running the shortcut, the containg folder becmes current. Itthen gets inherited. – Walter Mitty Mar 07 '19 at 12:39
  • @Jlisk: The only caveat re Walter's solution is that your users must open the shortcut either from the Desktop or via File Explorer - it won't work from the taskbar or the Start Menu. – mklement0 Mar 07 '19 at 16:09
  • 1
    Thanks for the update. Also for the caveat. I didn't think of opening from the taskbar. – Walter Mitty Mar 07 '19 at 19:16
1

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • @Jlisk: No, relative paths aren't possible - please see my update - but even they wouldn't solve your problem - Walter's answer is the way to go. – mklement0 Mar 07 '19 at 23:11
-1

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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-6

RSchreib
  • 121
  • 1
  • 1
  • 6
  • The `.TargetPath` property specifies what _executable_ to run (or document to open); in the OP's case, it is `powershell.exe`, so what you're proposing is unrelated to the OP's desire to have the `*.ps1` script that the shortcut invokes run in the same directory as the shortcut itself. – mklement0 Mar 04 '19 at 13:41