When you right-click PowerShell from the start menu and choose to run it as Administrator, the window's title becomes
.
to make title matching work on substrings or, if you want to, regex it up.
Here's my modification to your script that tries to enable your PS hotkeys even if you start the command prompt first and then start PowerShell in it:
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode, Input ; Recommended for new scripts due to its superior speed and reliability.
SetBatchLines, -1
;ListLines, Off
;#KeyHistory 0
;SetKeyDelay, -1, -1
#Warn
#If (WinActive("ahk_exe powershell.exe") || powershellIsPrimaryProcess(WinActive("ahk_exe cmd.exe")))
F1::
Send {Escape}Get-Help{Space}
Return ; Press F1 to insert Get-Help
F2::
Send {Escape}Get-Command{Space}
Return ; Press F2 to insert Get-Command
F3::
Send {Escape}Get-Alias{Space}
Return ; Press F3 to insert Get-Alias
F4::
Send {Escape}Get-ChildItem{Enter}
Return ; Press F4 to Get-ChildItem
F5::
Send {Escape}Clear-Host{Enter}
Return ; Press F5 to Clear-Host
F6::
Send {Escape}Get-History{Enter}
Send Invoke-History{Space}
Return ; Press F6 to Invoke-History
#If
powershellIsPrimaryProcess(cmdHwnd)
{
static scriptPID := DllCall("GetCurrentProcessId", "UInt")
,processCount := 10
,processList := 0
,basename := ""
,AttachConsole := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "AttachConsole", "Ptr")
,GetConsoleProcessList := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "GetConsoleProcessList", "Ptr")
,FreeConsole := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "FreeConsole", "Ptr")
ret := False
if (!cmdHwnd)
return False
WinGet targetCmdPID, PID, ahk_id %cmdHwnd%
if (targetCmdPID && DllCall(AttachConsole, "UInt", targetCmdPID)) {
if (!VarSetCapacity(processList))
VarSetCapacity(processList, processCount * 4, 0)
,VarSetCapacity(basename, 522)
Loop {
actualCapacity := DllCall(GetConsoleProcessList, "Ptr", &processList, "UInt", processCount, "UInt")
if (!actualCapacity) {
break
} else if (actualCapacity > processCount) {
processCount := actualCapacity
VarSetCapacity(processList, processCount * 4, 0)
continue
}
Loop %actualCapacity% {
dwPID := NumGet(processList, (A_Index - 1) * 4, "UInt")
if (dwPID == scriptPID || dwPID == targetCmdPID)
continue
if ((fullPath := QueryFullProcessImageName(dwPID))) {
SplitPath, fullPath, basename
if (basename == "powershell.exe") ; use single = for case-insensitive string compare
ret := True
}
break
}
break
}
DllCall(FreeConsole)
}
return ret
}
QueryFullProcessImageName(PID)
{
static imageName, PROCESS_QUERY_LIMITED_INFORMATION := 0x1000
,OpenProcess := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "OpenProcess", "Ptr")
,QueryFullProcessImageNameW := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "QueryFullProcessImageNameW", "Ptr")
,CloseHandle := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandleW", "WStr", "kernel32.dll", "Ptr"), "AStr", "CloseHandle", "Ptr")
if (!PID)
return ""
size := 260
if (!VarSetCapacity(imageName))
VarSetCapacity(imageName, (size + 1) * 2)
if (!(hProcess := DllCall(OpenProcess, "UInt", PROCESS_QUERY_LIMITED_INFORMATION, "Int", False, "UInt", PID, "Ptr")))
return ""
pathObtained := DllCall(QueryFullProcessImageNameW, "Ptr", hProcess, "UInt", 0, "WStr", imageName, "UInt*", size)
,DllCall(CloseHandle, "Ptr", hProcess)
return pathObtained ? imageName : ""
}