PowerShell: run the script from the shortcut by using a relative path

EDIT . For future readers, in short, PowerShell scripts are not intended to be used in this way, so there is no elegant solution.

I have the following line, which runs the script as an administrator of the label:

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, for example:

". \ Test.ps1"

but I do not understand how I can do it. How can I run a script in relation to the location of the label? (Label and script are in the same folder)

+6
source share
5 answers

Here is an ugly workaround.

Shortcut.lnk file with the Target: %COMSPEC% /C .\launcher.cmd ( source ) and start at: %CD% (or blank ).

Launcher.cmd with the contents of the file:

 Powershell -noprofile -noexit -File %CD%\PSlauncher.ps1 

PSlauncher.ps1 with the contents of the file:

 Start-Process Powershell -verb RunAs -ArgumentList ($pwd.path + "\test.ps1") 

Of course, there is a better solution. Perhaps, with the option -WorkingDirectory Etpu Start-the Process ? Or save the credentials by using cmdlets Convert * -SecureString? Consider me curious.

Why label required?

+3
source

After much trial and error I came up with a solution:

Create a shortcut with this (edited for your .ps1) so that the scripts run as admin in relation to any directory:

 CMD /C PowerShell "SL -PSPath '%CD%'; $Path = (GL).Path; SL ~; Start PowerShell -Verb RunAs -Args \""SL -PSPath '"$Path"'; & '".\YourScriptHere.ps1"'"\"" 

You will need to clear the β€œStart at” shortcut so that its relative path is set as the working directory.

Or here is a script that will generate one of these shortcuts for each .ps1 in the directory (with "Start on" already cleared):

 (GCI | Where-Object {$_.Extension -eq ".ps1"}).Name | ForEach-Object { $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut((GL).Path+"\$_ Run.lnk") $Shortcut.TargetPath = 'CMD' $Shortcut.Arguments = "/C PowerShell `"SL -PSPath `'%CD%`'; `$Path = (GL).Path; SL ~; Start PowerShell -Verb RunAs -Args \`"`"SL -PSPath `'`"`$Path`"`'; & `'`".\$_`"`'`"\`"`"" $Shortcut.IconLocation = 'PowerShell.exe' $Shortcut.Save() } ) .Path + "\ $ _ Run.lnk") (GCI | Where-Object {$_.Extension -eq ".ps1"}).Name | ForEach-Object { $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut((GL).Path+"\$_ Run.lnk") $Shortcut.TargetPath = 'CMD' $Shortcut.Arguments = "/C PowerShell `"SL -PSPath `'%CD%`'; `$Path = (GL).Path; SL ~; Start PowerShell -Verb RunAs -Args \`"`"SL -PSPath `'`"`$Path`"`'; & `'`".\$_`"`'`"\`"`"" $Shortcut.IconLocation = 'PowerShell.exe' $Shortcut.Save() } ; (GCI | Where-Object {$_.Extension -eq ".ps1"}).Name | ForEach-Object { $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut((GL).Path+"\$_ Run.lnk") $Shortcut.TargetPath = 'CMD' $Shortcut.Arguments = "/C PowerShell `"SL -PSPath `'%CD%`'; `$Path = (GL).Path; SL ~; Start PowerShell -Verb RunAs -Args \`"`"SL -PSPath `'`"`$Path`"`'; & `'`".\$_`"`'`"\`"`"" $Shortcut.IconLocation = 'PowerShell.exe' $Shortcut.Save() } 

If necessary, add -NoExit , -ExecutionPolicy Unrestricted etc. immediately after the first \" .

Notes:

The reason for the second instance of admin PowerShell, to run the first time, is that running as an administrator directly (by selecting the shortcut "Run as administrator"), to , for any reason, ignores the "Start" and always starts in the System32.

CMD is used to start the first instance because PowerShell currently cannot resolve paths containing square brackets, interpreting them as regular expression characters. This can usually be avoided by using LiteralPath parameter (aka PSPath), but here the path is passed behind the scenes at the start, and it is corrected by developers (I just sent a bug report here ).

+1
source

When I run the script from the shortcut, it uses the path to the actual script. You can check the current directory by pwd (current working directory). You can check (and then use) path to script with split-path -parent $MyInvocation.MyCommand.Definition , as a response to said in What is the best way to determine the location of the current PowerShell script? .

So, to answer your question, you should already be able to use relative paths. You tried it? If so, what was your experience?

0
source

For your script, set it as the default using Powershell. Create a shortcut for your script and assign a hotkey to it, right-clicking on the shortcut, selecting properties, go to the shortcuts tab. Move the cursor to the shortcut key and define a key combination. Each time you press the shortcut key, your script will run

0
source

It will definitely be harder than it is.

This problem is likely to affect you on Windows Server. On normal Windows, you can run Set-ExecutionPolicy unrestricted , and it will remain in force for a computer on Windows Server (at least on the AWS), setting the execution policy of the powershell script only for the session script and it is closed immediately, so you can not see the error.

I just successfully modeled registry AWS instance bypass Group Policy and can simply right-click powershell scripts from any directory and send the shortcut on the desktop that can run.

-1
source

All Articles