Running a PowerShell script from AHK when its path include space Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Running a PowerShell script from AHK when its path include space

13 Jun 2018, 08:44

How do you Run (or RunWait) a PowerShell script from an AHK script when its path includes space(s).

For example, this PS1 script (save as "pause.ps1":

Code: Select all

write-host "Press any key to continue..."
[void][System.Console]::ReadKey($true)
And this AHK script:

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically

strPsScriptFile := A_ScriptDir . "\pause.ps1"

RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command %strPsScriptFile%
return
When the A_ScriptDir does not include space, the PowerShell script runs well. But rename the folder and insert a space in its name and the script does not work.

Of course, I tried various combination of quoting, for example:
RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command "%strPsScriptFile%"
RunWait, PowerShell.exe -ExecutionPolicy Bypass "-Command %strPsScriptFile%"
RunWait, PowerShell.exe "-ExecutionPolicy Bypass -Command %strPsScriptFile%"

But none worked. I also tried to encode the path (replace space with "%20"), without success.

Any help, please?
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Running a PowerShell script from AHK when its path include space

13 Jun 2018, 09:09

RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command "%strPsScriptFile%"
OOPS, thats what you tried, sorry
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Running a PowerShell script from AHK when its path include space

13 Jun 2018, 12:59

Found it. We need to get the script file's "8.3 short path and name".

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically

strPsScriptFile := A_ScriptDir . "\pause.ps1"
Loop, %strPsScriptFile%
	if StrLen(A_LoopFileShortPath) ; in case the file system does not support 8.3 (NtfsDisable8dot3NameCreation)
		strPsScriptFile := A_LoopFileShortPath

RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command %strPsScriptFile%
return
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Running a PowerShell script from AHK when its path include space

13 Jun 2018, 13:53

Luck guy! The drive where I wanted to run my PS script does not support 8.3! I tested the script on another drive supporting it and it solved the issue.

If you know of another way to have a script with path/file name including spaces to be launched, let me know. This would make my solution work on a larger number of systems.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Running a PowerShell script from AHK when its path include space

15 Jun 2018, 09:28

Hi JnLlnd, the script you posted doesn't work on my system either. This is ugly but works:

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically

strPsScriptFile := ".\pause.ps1"

;Run, cmd /c cd C:\ && dir && pause ; For testing
Run, cmd /c cd %A_ScriptDir%
RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command %strPsScriptFile%
return
But of course if the .ps1 script is always going to be in the same folder as the AHK script, the

Code: Select all

Run, cmd /c cd %A_ScriptDir%
is superfluous.

EDIT: This works too, but with a different brand of ugly:

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically

strPsScriptFile := A_ScriptDir "\pause.ps1"
PsScriptFile := ".\pause.ps1"
RunWait, PowerShell.exe Set-Location %strPsScriptFile%, , Min
;RunWait, cmd /c dir && pause ; for testing
RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command %PsScriptFile%
return
Regards,
burque505
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Running a PowerShell script from AHK when its path include space  Topic is solved

15 Jun 2018, 10:11

Use the WorkingDir parameter.

Code: Select all

RunWait PowerShell.exe -ExecutionPolicy Bypass -Command .\pause.ps1, d:\  ; example in the directory d:
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Running a PowerShell script from AHK when its path include space

15 Jun 2018, 10:23

@burque505: Does it also work when the A_ScriptDir (or A_WorkingDir if we use it) includes a space in its path?
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Running a PowerShell script from AHK when its path include space

15 Jun 2018, 12:30

@JnLlnd, yes, I tested it on my system and it works. Although it chokes on "A_ScriptDir . "\pause.ps1", PowerShell can at least change directory with quotes around the path, if you do a Set-Location or "cd", and even if it couldn't cmd.exe of course can. :)
If you need to move around in the directory structure and back, you can always save the current directory to a variable, change to the directory where 'pause.ps1' is, run ".\pause.ps1" and then change back.
I have a directory "PS" the directory below is "PS\PS Spaces", as shown in the GIF.
Dir with spaces.PNG
Dir with spaces.PNG (7.69 KiB) Viewed 9144 times
Regards,
burque505
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Running a PowerShell script from AHK when its path include space

15 Jun 2018, 18:56

@burque505: Thanks for checking and for the additonnal info.

@Flipeador: Sorry, I overlooked your suggestion. This is actually working and is more straight forward solution. Thanks!
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Running a PowerShell script from AHK when its path include space

16 Jun 2018, 08:19

Hi, Flipeador's suggestion of course works perfectly, and I have a question or two, just for my own edification:
1) Will the script 'pause1.ps' always be in the same directory as the AHK script? Because if it is, I don't believe you need the Target Directory or A_WorkingDir parameter at all.
From the docs on the RunWait command:
WorkingDir

The working directory for the launched item. Do not enclose the name in double quotes even if it contains spaces. If omitted, the script's own working directory (A_WorkingDir) will be used.
2) If the AHK script will be some undetermined place, but you either know the location of the script 'pause.ps1' for sure or you can pass that location as a variable, then this would work, I believe:

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically
strPsScript := "C:\Users\burque505\PS\PS spaces" ; this being the directory containing pause.ps1
RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command ./pause.ps1, %strPsScript%
return
Regards,
burque505
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Running a PowerShell script from AHK when its path include space

16 Jun 2018, 08:40

burque505 wrote:Will the script 'pause1.ps' always be in the same directory as the AHK script? Because if it is, I don't believe you need the Target Directory or A_WorkingDir parameter at all.
In AHKv1 A_WorkingDir it is not always equal to A_ScriptDir.
autohotkey.com/docs/commands/SetWorkingDir.htm wrote:A script's initial working directory is determined by how it was launched. For example, if it was run via shortcut -- such as on the Start Menu -- its working directory is determined by the "Start in" field within the shortcut's properties.
burque505 wrote:If the AHK script will be some undetermined place, but you either know the location of the script 'pause.ps1' for sure or you can pass that location as a variable, then this would work, I believe:
it should work... :|

Code: Select all

MsgBox % PowerShell("./pause.ps1", "C:\Users\burque505\PS\PS spaces")  ; does not work if the file name contains spaces
ExitApp

PowerShell(Script, WorkingDir := "", Options := "", Params := "-ExecutionPolicy Bypass")
{
    Run % "PowerShell.exe " . Params . " -Command &{" . Script . "}", % WorkingDir == "" ? A_ScriptDir : WorkingDir, % Options, PID
    Return PID
}
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Running a PowerShell script from AHK when its path include space

16 Jun 2018, 09:10

Note: I think it works if you enclose the path with spaces in single quotes.

Code: Select all

MsgBox % PowerShell("C:\Users\burque505\PS\PS spaces\pause.ps1")
ExitApp

PowerShell(Script, Options := "", Params := "-ExecutionPolicy Bypass")
{
    SplitPath Script, psfile, WorkingDir
    ;MsgBox % psfile "|" WorkingDir
    Run % "PowerShell.exe " . Params . " -Command &{./'" . psfile . "'}", % WorkingDir == "" ? A_ScriptDir : WorkingDir, % Options, PID
    Return PID
}

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically

strPsScriptFile := A_ScriptDir . "\pause.ps1"

RunWait, PowerShell.exe -ExecutionPolicy Bypass -Command '%strPsScriptFile%' ;OR -Command &{'%strPsScriptFile%'}
return
> AHKv2 Test <
Note: PowerShell is enabled by default starting with WIN_8.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Frogrammer, mikeyww and 265 guests