Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Custom: ahk() - Dynamically run AHK code in PowerShell

  • Please log in to reply
2 replies to this topic
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

First off, I'm not sure where to post this as the Scripts Custom subforum is gone. Hopefully the mods would move this to correct forum(in any case it should not be here)


After downloading Git the other day, I discovered Windows PowerShell. I've heard about it but I was not aware that it comes with the OS. After playing with it for a couple of days, I came up with this function. This not written in AHK but rather in PowerShell scripting language(the original version uses both AHK and PowerShell but I rewrote it to optimize the purpose and reduce dependencies). This allows the user to dynamically run AHK code from within the console.


How it works:

It's basically HotKeyIT's DynaRun converted to a PowerShell script.



Tested on Windows 7 Ultimate(x86) using AHK v. and PowerShell 2.0. I have not tested this with the ANSI version of AHK. See below for workaround.



Copy the function and paste it in your PowerShell Profile. Start PowerShell, then type your AHK code:

PS C:\> $code =
>@" ; the @" ... "@ is sort of PowerShell's version of continuation section
>Gui, Add, Edit, w300 r5
>Gui, Add, Button, w88 h26, OK
>Gui, Show


Then call the function:

PS C:\>ahk $code ; pass the variable containing the AHK code(string)


That's itwink.png


Function: (Note: This is not written in AHK)

function ahk($script)
    $pipeName = "AHK_" + [System.Environment]::TickCount
    $pipeDir = [System.IO.Pipes.PipeDirection]::Out
    $maxNum = [Int]254
    $pipeTMode = [System.IO.Pipes.PipeTransmissionMode]::Message
    $pipeOptions = [System.IO.Pipes.PipeOptions]::None
    $ahkPath = [Environment]::GetFolderPath("ProgramFiles") + "\AutoHotkey\AutoHotkey.exe"
    $pipe_ga = new-object System.IO.Pipes.NamedPipeServerStream($pipeName, $pipeDir, $maxNum, $pipeTMode, $pipeOptions)
    $pipe = new-object System.IO.Pipes.NamedPipeServerStream($pipeName, $pipeDir, $maxNum, $pipeTMode, $pipeOptions)
    if ($pipe_ga -and $pipe) {
        Start-Process $ahkPath "\\.\pipe\$pipeName"
        $script = [char]65279 + $script
        $sw = new-object System.IO.StreamWriter($pipe)
    } else { Write-Host "Operation cancelled: Failed to create named pipe" }

For the ANSI version of AHK:

Replace this line:

$script = [char]65279 + $script


$script = [char]239 + [char]187 + [char]191 + $script



- Thanks to HotKeyIT for DynaRun

- Thanks to Lexikos for http://www.autohotke...through-a-pipe/


I haven't written any PowerShell script nor have any knowledge of .NET so pardon any mistakes in implementation, code, etc. Comments are very much welcome.wink.png


In action:


  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

very nice, and i def think it belongs here

  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

@guest3456: Thankshappy.png