Skip to content

Instantly share code, notes, and snippets.

@rabel001
Last active November 5, 2024 17:16
Show Gist options
  • Save rabel001/6317a5321f8df50b213928b8813e47cf to your computer and use it in GitHub Desktop.
Save rabel001/6317a5321f8df50b213928b8813e47cf to your computer and use it in GitHub Desktop.
AutoHotKey Embed PowerShell
#SingleInstance, force
SetTitleMatchMode 2
SetKeyDelay, 10, 10
SetBatchLines, -1
;---------------------------------------------------------------------------
; Button Library found at https://github.com/AHK-just-me/Class_ImageButton
;---------------------------------------------------------------------------
;#Include ..\lib\Class_ImageButton.ahk
#Include <Class_ImageButton>
ThisTitle = PowerShell
InputBox, command, % ThisTitle, Type a PowerShell command.`nExample: "Get-Process"
if errorlevel 0
Gosub, DONE
else
gosub, CALLPWRSHLL
return
CALLPWRSHLL:
; Styles we want to remove from the console window:
WS_POPUP := 0x80000000
WS_CAPTION := 0xC00000
WS_THICKFRAME := 0x40000
WS_EX_CLIENTEDGE := 0x200
; Styles we want to add to the console window:
WS_CHILD := 0x40000000
; Styles we want to add to the Gui:
WS_CLIPCHILDREN := 0x2000000
; Flags for SetWindowPos:
SWP_NOACTIVATE := 0x10
SWP_SHOWWINDOW := 0x40
SWP_NOSENDCHANGING := 0x400
; Create Gui and get window ID.
Gui, PS:Color, White
Gui, PS:Font, s10 bold, Segoe UI
Gui, PS: +LastFound +%WS_CLIPCHILDREN%
GuiWindow := WinExist()
; Launch hidden cmd.exe and store process ID in pid.
PShellCmd := "&{"
. "[console]::BackgroundColor=[System.ConsoleColor]::Black;"
. "[console]::ForegroundColor=[System.ConsoleColor]::Green;"
. "[console]::WindowHeight=37;"
. "[console]::WindowWidth=100;"
. "[console]::BufferHeight=300;"
. "[console]::BufferWidth=100;"
. "[console]::Title=`'" ThisTitle "`';"
. "cls;"
. "}"
Run powershell -execution bypass -noexit -command %PShellCmd%,,hide,pid
; Wait for console window to be created, store its ID.
DetectHiddenWindows, On
WinWait, ahk_pid %pid%
ConsoleWindow := WinExist()
sleep 1000
; Get size of console window, excluding caption and borders:
VarSetCapacity(ConsoleRect, 16)
DllCall("GetClientRect", "uint", ConsoleWindow, "uint", &ConsoleRect)
ConsoleWidth := NumGet(ConsoleRect, 8, "int")
ConsoleHeight:= NumGet(ConsoleRect, 12, "int")
; Apply necessary style changes.
WinSet, Style, % -(WS_POPUP|WS_CAPTION|WS_THICKFRAME)
WinSet, Style, +%WS_CHILD%
WinSet, ExStyle, -%WS_EX_CLIENTEDGE%
; Put the console into the Gui.
DllCall("SetParent", "uint", ConsoleWindow, "uint", GuiWindow)
; Move and resize console window. Note that if SWP_NOSENDCHANGING
; is omitted, it incorrectly readjusts the size of its client area.
DllCall("SetWindowPos", "uint", ConsoleWindow, "uint", 0
, "int", 65, "int", 40, "int", ConsoleWidth, "int", ConsoleHeight
, "uint", SWP_NOACTIVATE|SWP_SHOWWINDOW|SWP_NOSENDCHANGING)
; Show the Gui. Specify width since auto-sizing won't account for the console.
; Add a button below the console.
btnWhere := "y" ConsoleHeight-110
;Gui, PS:Add, Button, x50 %btnWhere% w100 h24, Close
Gui, PS:Add, Button, vBT1 x50 %btnWhere% w100 h24 hwndhBT1, Close
Opt1 := [0, 0x0052D017, , 0x00FFFFFF, 2, , 0x0052D017, 2]
Opt2 := [, 0x00FFFFFF, , 0x0052D017]
Opt5 := [ , , ,0x00FFFFFF]
if !(ImageButton.Create(hBT1, Opt1, Opt2, , , Opt5))
MsgBox, 0, ImageButton Error btn1, % ImageButton.LastError
; Show the Gui. Specify width since auto-sizing won't account for the console.
cw := "W" ConsoleWidth-100
Gui, PS:Show, xCenter yCenter %cw%, % ThisTitle
; Be sure to close cmd.exe later.
OnExit, Exiting
gosub, SENDCMD
Process, WaitClose, %pid%, ;This waits to close the console to move on so you can manually type in some stuff if you want
gosub, DONE
SENDCMD:
if %command%
{
SendRaw, %command%
Send, {Enter}
}
return
DONE:
Gui, PS:Destroy
;MsgBox, Exited...
ExitApp
PSButtonClose:
PSGuiEsape:
PSGuiClose:
Exiting:
OnExit
Process, Close, %pid% ;May be a bit forceful? No effect if it already closed.
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment