How can I get the path of a Windows shortcut that runs my PowerShell script?

0
0
Asked By CuriousCoder42 On

I'm trying to figure out how to retrieve the path of the Windows shortcut that calls my PowerShell script. My goal is to delete that shortcut after the script runs, but since the shortcut could be located anywhere, I'm unsure how to get this information. Can anyone guide me on passing this shortcut path as a parameter or accessing the shortcut path directly within PowerShell?

4 Answers

Answered By CodeNinja77 On

If you're looking to do something like allow the script to run just once, that's a bit tricky. Shortcuts don’t communicate back to PowerShell, so you might consider using a command-line argument when launching the script. A simpler way to manage script runs could be to use a system where you check for a ticket or a record to see if it has been executed.

Answered By ScripterJoe99 On

The Windows shortcut doesn’t provide that kind of direct feedback. You could explore using `$PSBoundParameters`, `$PSScriptRoot`, and `$MyInvocation` for some details, but if you require certainty, try using a batch file that utilizes `%~dp0` to execute your PowerShell script.

Answered By PowerGeek101 On

To get the shortcut path from your script, you might want to check out the `Get-StartupInfo` function from jborean93's `ProcessEx` module. You can find out whether your script was launched via a shortcut by checking certain flags.

Answered By TechWhiz88 On

Unfortunately, the script itself can't inherently know if it was launched through a shortcut or what that shortcut's path is. You'd either need to pass that information as a parameter when calling the script, but that creates a bit of a problem because the code calling the script would also need to know where to get the shortcut path from, which can feel like a catch-22. Separating the script execution and shortcut removal might be the best route here.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.