• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • Forums
  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » Backend (Server-Side) » How To Execute PowerShell Scripts From PHP

How To Execute PowerShell Scripts From PHP

Last updated on September 10th, 2020 at 01:15 pm by Ehi Kioya Leave a Comment

Since PowerShell is now open source and cross-platform, we can now perform PowerShell magic right inside Linux boxes. We can execute PowerShell scripts (or commands) directly from within PHP code on Linux machines.

We can even place PowerShell commands right inside our PHP code without using an external script.

No more dependence on Windows/Microsoft operating systems! If this isn’t exciting stuff, what is?!

Before you can execute PowerShell scripts from PHP on a Linux machine however, you first need to install PowerShell Core on the Linux machine in question. My previous article discussed how to do that on Ubuntu.

If you have already installed PowerShell on your Linux box, you can use a number of similar PHP commands to execute PowerShell code.

Any of these commands should work:

/*
.
*/
shell_exec('pwsh -File /your/path/to/file.ps1');

// Or
exec('pwsh -File /your/path/to/file.ps1');

// Or
system('pwsh -File /your/path/to/file.ps1');

// Or
passthru('pwsh -File /your/path/to/file.ps1');
/*
.
*/

The commands are all similar with slight differences in their behavior:

  • shell_exec returns the full output of the command at the end of execution. Not just the last line of generated output.
  • exec returns only the last line of generated output during program execution.
  • system displays all code execution output immediately. It is used to show text data.
  • passthru also displays all code execution output immediately. But it is used for binary (or raw) data.

With both shell_exec and exec, you can handle the output yourself, but system and passthru displays the output immediately and will not let you customize it.

The pwsh command is used to start Powershell. In the above examples, we’re passing a .ps1 file to be executed.

You can also execute a PowerShell command directly by using -Command flag instead of the -File flag which was used above.

“Non-Blocking” PowerShell Execution In PHP

If you want to trigger a PowerShell process via PHP without “blocking” your PHP process, execute the script like this:

shell_exec('pwsh -File /your/path/to/file.ps1 > /dev/null 2>&1 &');

When coded like that, PHP will simply start the PowerShell process and move on to executing the next line of PHP code without waiting for the Powershell script to finish executing. No messages from the PowerShell process will be displayed.

I find this useful in certain scenarios, like firing off a request to deallocate an Azure VM.

When I deallocate VMs, I usually don’t want my PHP process to wait until the deallocation completes.

I like to send the command and then move on to other stuff. The /dev/null construct used above helps to achieve this.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: Backend (Server-Side), Linux & Servers, PHP, PowerShell, Programming Tagged With: Linux, PHP, PowerShell

About Ehi Kioya

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

25,822
Followers
Follow
30,000
Connections
Connect
14,617
Page Fans
Like

POPULAR   FORUM   TOPICS

  • How to find the title of a song without knowing the lyrics
  • Welcome Message
  • How To Change Or Remove The WordPress Login Error Message
  • The Art of Exploratory Data Analysis (Part 1)
  • Getting Started with SQL: A Beginners Guide to Databases
  • Replacing The Default SQLite Database With PostgreSQL In Django
  • How to Implement Local SEO On Your Business Website And Drive Traffic
  • Understanding Routes In Laravel
  • Building A Blog With Laravel – Part 6: Creating A Form For New Posts
  • Getting Started With JupyterLab
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library
    This article explains how to bypass the single user remote desktop connection restriction on Windows 10 by using the RDP wrapper library.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • Forums
  • About
  • Contact

© 2021   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy