AutoHotkey 2 how to get return value from powershell script

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
rovingwind
Posts: 2
Joined: 19 Mar 2023, 22:50

AutoHotkey 2 how to get return value from powershell script

19 Mar 2023, 23:00

I have a test.ps1 like below

Code: Select all

$input = $args[0]
return $input
how to run the powershell script and get the return value

Code: Select all

#c::{  
Run 'pwsh test.ps1 2', , 'Hide'
MsgBox returnValue
}
[Mod edit: Moved topic from 'Scripts and Functions'.]
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: AutoHotkey 2 how to get return value from powershell script

20 Mar 2023, 07:03

I'm not an expert in this, but this could be a step in the right direction.

Code: Select all

#Requires AutoHotkey v2.0
psScript := A_ScriptDir '\test.ps1'
shell    := ComObject('WScript.Shell')
exec     := shell.Exec(A_ComSpec ' /Q /K echo off')
param    := 2
exitPS   := '`nexit'
commands := 'powershell ' psScript ' ' param exitPS
exec.StdIn.WriteLine(commands)
MsgBox exec.StdOut.ReadAll()

Code: Select all

param(
     $message
)
return $message
There could be a more straightforward way to get the value returned from Run.
rovingwind
Posts: 2
Joined: 19 Mar 2023, 22:50

Re: AutoHotkey 2 how to get return value from powershell script

07 Apr 2023, 15:00

accroding to official document

Code: Select all

RunWaitOne(command) {
    shell := ComObject("WScript.Shell")
    exec := shell.Exec(A_ComSpec " /C " command)
    return exec.StdOut.ReadAll()
}
it works fine but cmd window will show momently
I try to hide cmd window by below script, but it can't get the execut result.

Code: Select all

exec := shell.Exec(A_ComSpec " /C " " hide " command)
How to get execute result and not showing the cmd window?

@mikeyww
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: AutoHotkey 2 how to get return value from powershell script

07 Apr 2023, 15:37

I'm not sure how to hide it, but some other folks here probably know and can reply.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: macromint and 105 guests