Question:
EDIT: To future readers, in short, PowerShell scripts weren’t intended to be used this way which is why there is no elegant solution.
I have the following line which runs a script as an administrator from a shortcut:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile
-noexit Start-Process Powershell -verb RunAs -ArgumentList “C:\Documents\WindowsPowerShell\Scripts\test.ps1”
I want to change:
“C:\Documents\WindowsPowerShell\Scripts\test.ps1”
to a relative path like:
“.\test.ps1”
but I haven’t figured out how I can do that. How can I run the script relative to the location of the shortcut? (The shortcut and script are in the same folder)
Answer:
Here is an ugly workaround.
Shortcut.lnk
file with Target: %COMSPEC% /C .\launcher.cmd
(source) and Start In: %CD%
(or blank).
Launcher.cmd
file with contents:
1 2 |
Powershell -noprofile -noexit -File %CD%\PSlauncher.ps1 |
PSlauncher.ps1
file with contents:
1 2 |
Start-Process Powershell -verb RunAs -ArgumentList ($pwd.path + "\test.ps1") |
Surely there is a better solution. Maybe with the -WorkingDirectory
parameter of Start-Process? Or storing credentials with the Convert*-SecureString cmdlets? Count me curious.
Why do you want a shortcut?