Path to script shortcut

I use shortcuts to launch my scripts (mostly because -executionpolicy bypass is so convenient) and obviously PowerShell is aware of the name of this shortcut, since the title bar of the console includes it. But how does one get to this path from within the script? I can get the path of the actual ps1 file that is running, but I haven't found a way to get to the calling shortcut path.

Also, a little off topic, but is it possible to provide the path to the icon for a shortcut as a relative path? So that the icon can be provided in the folder structure, and the folder can go anywhere? Or is the icon path a dumb/literal path?

My goal is to provide my own icon files, in the folders provided, with shortcuts that reference those icons. If I could do a relative path I would be done. Failing that I can provide a shortcut and have my script trace back the calling shortcut and then update the icon with the current path. If the folder gets moved the icons are broken, until first use, when they are properly repathed again.

January 21st, 2015 1:28am

Your Powershell script will not be aware of the shortcut location - all it knows is it's actual invocation path with is automatically saved in the PSScriptRoot variable.

As for the icon - I am not certain whether these accept relative paths instead of absolute paths. That's not really a Powershell issue. 

I think your best best would be to use batch files instead of shortcuts. Batch files will be aware of their own path and you can call a Powershell script in another path from the batch file like:

Powershell.exe -command {& <path to script>} -ExecutionPolicy RemoteSigned

I assume the reason for all this is so you can publish scripts for others to use? Another but more difficult solution would be to create a graphical tool using WPF or Windows forms that has buttons or a picker menu of some sort to choose what they would like to run and prompt for input where necessary.

A third option would be  to have one script as a shortcut the way you are doing it now and that script can just be a front-end menu for your other scripts and can accept console input where necessary.

Free Windows Admin Tool Kit Click here and download it now
January 21st, 2015 1:48am

Hmm. Thats a bummer, because the info is there somewhere. When I have two shortcuts with different names, pointing to the same script, I see the shortcut name after the user name in the console title bar. But just because the console knows, doesn't mean we have access to it from within the script.

In my case the shortcut approach is the right one, for myriad other, more important reasons. The icon would just help identify the two kinds of shortcuts, the ones with arguments where the user will copy the shortcut and change the arguments to create a new process, and the ones that the user should only use as provided. 

That, and the fact that the assigned shortcut also shows up in the task bar is a nifty branding opportunity. ;)

EDIT: This is interesting. I had done some tweaking of the console to get a white background and nicer colors for my needs, and that got me thinking. Some Google Fu led me to $host.ui.rawui.windowtitle, and it turns out is isn't the user name, it just says Administrator: in front of the shortcut name. I need to test this logged in as a regular user, but as a starting point this works.

$ShortcutName = $host.ui.rawui.windowtitle -replace "Administrator: ", ""

I can also look at revising the title, so instead of Administrator it could be some pertinent information, plus probably still the shortcut name.
January 21st, 2015 2:01am

Yes you can get the title from $host, but this is not the full path, just the file name of the shortcut.

Also, it looks like shortcuts support relative paths by using '.' - so the path would look like '.\script.ps1'

Free Windows Admin Tool Kit Click here and download it now
January 21st, 2015 2:24am

In my case the shortcut approach is the right one, for myriad other, more important reasons. The icon would just help identify the two kinds of shortcuts, the ones with arguments where the user will copy the shortcut and change the arguments to create a new process, and the ones that the user should only use as provided. 

This seems like a convoluted path to get what may be a simple goal - shareable scripts. Powershell has native ways for users to modify the arguments provided. These are parameters.
January 21st, 2015 2:26am

Indeed, it isn't a full path, but since I know where the shortcuts are relative to the script, so I can extrapolate the full path and sweeten the icons for those shortcuts that stay in my sandbox. If a user adds a full path and moves it out of my folders they just need to do it after the shortcut is run once from the relative path, and as long as the full path doesn't change the icon will persist.

As for relative paths, the script path itself can be, but so far as I can tell the icon path cannot. But maybe that's a limitation of the UI for assigning it. Will need to try writing a bit of code to force a relative path in there and see what happens. Interesting thought, that. And for another day. I need to go to bed before the sun comes up. ;)

Free Windows Admin Tool Kit Click here and download it now
January 21st, 2015 2:30am

Hello ThisPublicIdentityIsMine,

This may be a little late.

Another way to do it is to add the icon file folder to the PATH environment variable.
If the icon file is in a folder on the PATH then you don't require a folder path (at least it works in Windows 8).
To drag and drop folder paths you can download pathmgr.cmd  from  Technet. You can add the current path of pathmgr to the environment path with the command pathmgr /path /+ (use /- to delete pathmgr path).

To get help with the commands use pathmgr /?.

 For drag and drop operation you can create a shortcut file  and include the following target - "pathmgr.cmd /add /y". This will add the dropped path to  the user path in the registry without prompting.

pianoboy


  • Edited by pianoboy 11 hours 16 minutes ago correction
May 27th, 2015 3:56pm

Hello ThisPublicIdentityIsMine,

This may be a little late.

Another way to do it is to add the icon file folder to the PATH environment variable.
If the icon file is in a folder on the PATH then you don't require a folder path (at least it works in Windows 8).
To drag and drop folder paths you can download pathmgr.cmd  from  Technet. You can add the current path of pathmgr to the environment path with the command pathmgr /path /+ (use /- to delete pathmgr path).

To get help with the commands use pathmgr /?.

 For drag and drop operation you can create a shortcut file  and include the following target - "pathmgr.cmd /add /y". This will add the dropped path to  the user path in the registry without prompting.

pianoboy


  • Edited by pianoboy Wednesday, May 27, 2015 7:53 PM correction
Free Windows Admin Tool Kit Click here and download it now
May 27th, 2015 7:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics