Using Powershell script from PHP

The information from this blog POST has been compiled with the help of the following engineers:

  • Regis DAMONGEOT – société SMILE
  • Theve THAVASOTHY – société STERIA
  • Gregory CHARLET – société STERIA

There are various blogs & examples on the Internet showing how to use PowerShell in ASP.NET. A typical usage scenario consists to use Exchange PowerShell scripts from a web server application in order to manage mailboxes. An example of this can be found here : HOWTO: Using PowerShell in ASP.NET (.NET Framework 2.0). It's also possible to use PowerShell from PHP but, in this case, you have to be aware of the following caveats:

  1. The Powershell script called from PHP may hang when STDIN is redirected (this is typically the case when PHP is used as a CGI process). This problem is documented in a few places :

    executing a Powershell script from php
    PowerShell.exe can hang if STDIN is redirected

    In order to avoid the hang, you'll need to use the undocumented parameter "-InputFormat none" when your PHP script instantiates Powershell:

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


  2. If the Powershell script you are using from PHP is .Net based (Exchange PowerShell scripts are using managed code), you may face a second hang scenario. If you debug this hang or get a network trace, you'll notice that the hang is caused by the .Net framework trying to fetch a CRL ( This issue is described in this blog: Make sure you have an Internet connection when using signed assemblies and similar symptoms can be found in this article: Exchange Server 2007 managed code services do not start after you install an update rollup for Exchange Server 2007. One way to resolve this issue consists to create a manifest file for Powershell in order to prevent signed assemblies signature checks :


            <generatePublisherEvidence enabled="false"/>

Please note that the above information applies if you are calling Powershell script from PHP on either IIS or Apache. We hope this blog will save you some research time!

Emmanuel Boersma


Comments (1)

  1. PD Subraa says:

    I have faced this hang scenarios multiple times and my colleagues had figured it out. Never knew its just simple thing.

    Thank you for this awareness!

Skip to main content