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
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.
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.
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.
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
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String