Run powershell in php


I have a very small program which I want to execute upon button press . In html it contains an button with an image and when i press the button it should call my php script which has powershell and has to be executed . But it doesnt add the printer through the manual command attached is working.

My html code here is as follows :

<form action="rabbit.php" method="POST">
<input type="image" src="image/GOOGLE.jpg" value="submit" alt="Submit" width="78" 

Php code :

  echo powershell.exe (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\\file-computer-01\IN-SEA-GOOGLE");

Even I tried in this way and it just echo's the command :

 $psPath = 'c:\\Windows\\System32\WindowsPowerShell\v1.0\\powershell.exe';
 $psDIR = "c:\\wamp\\www\\printer\\scripts\\";
 $psScript = "rabbit.ps1";
 $runScript = $psDIR. $psScript;
 $runCMD = $psPath.  "&". $runScript;
 echo $runCMD;

Moreover I get the output as in the page as :


My powershell code contains like this :

(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\file-computer-01\IN-SEA-GOOGLE")

If I execute the direct powershell script it works but not via PHP .

In both the cases, it just gives echo and doesnt add the printer .

Any ideas or suggestions please.... as i new to php struggling a lot to figure it out this.





It just echo's the command because that's what your code tells it to do :) Try replacing

echo $runCMD;


echo exec($runCMD);

PS also you will probably need to add the ExecutionPolicy directive to your command, because by default script execution is prohibited. So your command should be:

$psPath = 'c:\\Windows\\System32\WindowsPowerShell\v1.0\\powershell.exe';
$psDIR = "c:\\wamp\\www\\printer\\scripts\\";
$psScript = "rabbit.ps1";
$runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript;

exec($runCMD, $out);
echo join($out);



You probably want PHP's system function. Or perhaps its popen...

People are also looking for solutions to the problem: php - SCREAM: Error suppression ignored for Fatal error: Call to undefined method


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.