Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 663637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:30:41+00:00 2026-05-13T23:30:41+00:00

Trying to run the following command in php to run powershell command… the following

  • 0

Trying to run the following command in php to run powershell command…

the following works:

$output = shell_exec(escapeshellcmd('powershell get-service | group-object'));

I can not run it like this:

$output = shell_exec('powershell get-service | group-object');

it will not pass the pipe | character

but if I try to run:

$output = shell_exec(escapeshellcmd('powershell get-service | where-object {$_.status -eq "Running"}'));

I get no output.

The following:

$cmd = escapeshellcmd('powershell get-service | where-object {$_.status -eq "Running"}');

returns:

powershell get-service ^| where-object ^{^$_.status -eq ^"Running^"^}

Any suggestions on why this is happening and how to fix this?

Edit: Also I could run it as .ps1 script but I want to be able to pass $var to it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:30:42+00:00Added an answer on May 13, 2026 at 11:30 pm

    I’ll take a stab although I have no PHP experience whatsoever.

    I have a feeling that what’s happening is your pipe character is being interpreted by the command shell instead of PowerShell. For example if you ran the following at the cmd.exe command prompt:

    dir /s | more
    

    The output of the first command gets piped to the input of the second just like you’d expect in PowerShell.

    Escaping the string will only make the problem worse because you’re transforming the string in such a way that PowerShell has no idea how to unescape it.

    Try enclosing your original PowerShell expression in a quote like the following:

    $output = shell_exec('powershell.exe -c "get-service | group-object"');
    

    Or preferably, it looks like there’s an exec() function that does not go through the command shell. This might work better.

    $output = exec('powershell.exe -c get-service | group-object');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to run the following command on PowerShell: netsh http add sslcert
I'm trying to run the following command in terminal on my macbook pro cd
I'm trying to run the following PowerShell script to create a local user. This
So, I set up a cronjob to run the following command: php /var/www/path/to/cron/do-stuff.php The
I am trying to run the following command: [root@localhost certs]# openssl s_client -connect localhost:7043
I'm trying to run the following Web Deploy command: msdeploy.exe -verb:dump -source:dirPath=C:\Deploy,wmsvc=localhost,username=<user>,password=<pass> I have
I am trying to run some PHP pages on the command line but am
I am trying to run the following command on a very large text file.
I am trying to run the following. <?php $db = mysqli_connect(localhost,user,pw) or die(Database error);
I'm trying to run the following command using the 'exec' task in Ant: ls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.