executing a Powershell script from php executing a Powershell script from php powershell powershell

executing a Powershell script from php


It looks like your command is surrounded by single-quotes. I think if you remove them, your command should run.

shell_exec returns the output from the command you run. To further diagnose, store the output in a variable, then print it out:

$output = shell_exec($runCMD);echo '<pre>' . $output . '</pre>';

Make sure you enable running scripts. That capability is turned off by default. You have to enable the execution of scripts on each machine you want to run PowerShell scripts. Run about help_signing for more information.

Microsoft recommends running Set-ExecutionPolicy RemoteSigned -Scope LocalMachine. This allows all user accounts on a machine to run local scripts without issue, but requires confirmation to run scripts downloaded from the internet. This needs to be run in an administrative prompt. If you are running a 64-bit operating system, you'll need to do this from both a 64-bit and 32-bit shell.


Found this on another website and thought I would pass it along:

I was debugging a program that uses Windows API (Creating a Child Process with Redirected Input and Output) to capture stdout of Microsoft’s Windows PowerShell.

Script passed to PowerShell (-File switch) didn’t execute and PowerShell just hanged until killed by Task Manager.

It turns out that you need to use undocumented parameter “-InputFormat none”:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -InputFormat none -File file.ps1

This did the trick for me.


Use "-executionPolicy Unrestricted" along with the command "powershell.exe". Therefore the command will be :

powershell.exe -executionPolicy Unrestricted

Then it will surely work.