I often find that it's just quicker and easier to wrap a PowerShell 'one-liner' code block in AHK than try to do the same using nothing but AHK. (I accept that I'm a rubbish and lazy AHK programmer. )
Here's another 'gotcha' I ran into that I thought I would document to save others from pulling their hair out.
Be aware that the default version of PowerShell in Windows 7 is v2... and thus has no support for many cmdlets available in either PowerShell v3, v4 or v5.
For example, it took me ages to work out why the following 'Unblocker' script worked perfectly well in Windows 10 but didn't work at all in Windows 7:
; This script was prompted by https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/10/powertip-use-powershell-to-unblock-files-in-folder/
#SingleInstance force
; Prompt to 'Run as Admin', i.e. show UAC dialog
If Not A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%" ; Requires v1.0.92.01+
ExitApp
}
; Create the GUI elements
Gui, Add, Text, x20 y10 w170 h20 , Select SOURCE folder
Gui, Add, Button, x25 y30 w100 h30 , SOURCE
Gui, Add, Button, x25 y65 w100 h30 , Cancel
Gui -SysMenu
Gui, Show, w150 h100, Windows File Unblocker
return
; Determine what the SOURCE button does
ButtonSOURCE:
FileSelectFolder, SourceVar, *%A_MyDocuments%, 4, Select a SOURCE folder ;Select a source folder (with default location)
; Submit the results of the user's choice
Gui, submit ;Save the selected folder path to a variable
;MsgBox %SourceVar% ;Use MsgBox to test output to commandline
Gui, Destroy ;Close the first GUI so it doesn't muck up the progress bar
; Create and run the PowerShell command
psScript =
(
Get-ChildItem -Path '%SourceVar%' -Recurse | Unblock-File
)
Run PowerShell.exe -Command &{%psScript%} ,, hide
; Create a second GUI showing looping progress bar ('cos unblocking loads of files can take some time)
; Credit to polyethene for the progress bar routine (https://autohotkey.com/board/topic/12306-infinite-progress-bar/#entry79882)
Gui, Add, Progress, vlvl -Smooth 0x8 w350 h18 ; PBS_MARQUEE = 0x8
Gui, Show, , Unblocking... Please wait...
SetTimer, Unblocking, 45
Return
Unblocking:
GuiControl, , lvl, 1
Process, Exist, powershell.exe, ; Check whether PowerShell still running
If (!Errorlevel){
SetTimer, Unblocking, Off ; Turn off process timer
Gui, Destroy ; Close the second GUI
MsgBox, 64, Windows File Unblocker, Finished! Your files have been unblocked! ; Inform user that process has finished
;Run, %SourceVar% ; Open the folder to show files
ExitApp
}
Return
; Determine what the CANCEL button does
ButtonCancel:
ExitApp
Esc::ExitApp ; Use the Esc key to exit the app
The answer turned out to be that the default version of PowerShell built-in to Windows 7, i.e. v2.0, doesn't support Get-ChildItem -Path '%SourceVar%' -Recurse | Unblock-File. Eventually I found out that I needed to install PowerShell v3 for it to work.
So... if your PowerShell code wrapped in AHK doesn't work, consider building in a check for the version of PowerShell on the target device.