Execute Powershell script from Node.js Execute Powershell script from Node.js powershell powershell

Execute Powershell script from Node.js


You can just spawn a child process "powershell.exe" and listen to stdout for command output and stderr for errors:

var spawn = require("child_process").spawn,child;child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]);child.stdout.on("data",function(data){    console.log("Powershell Data: " + data);});child.stderr.on("data",function(data){    console.log("Powershell Errors: " + data);});child.on("exit",function(){    console.log("Powershell Script finished");});child.stdin.end(); //end input


The newer way to do this

const { exec } = require('child_process');exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {    // do whatever with stdout})


In addition to the accepted answer, there is a Node.JS Library called Edge.js that allows various langugages to be executed from within Node. Including C#, J#, .Net, SQL, Python, PowerShell and other CLR languages.

Note that Edge.js requires PowerShell 3.0 & only works on Windows (many of the other features work on Mac and Linux too).