I've been looking around the web and on Stackoverflow but hadn't found an answer to this question. How would you execute a Powershell script from Node.js? The script is on the same server as the Node.js instance.
4 Answers
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
7 Comments
Vandervidi
I know this is old but can the powershell be run with admin permissions?
muffel
@Vandervidi have a look at the node-windows npm package. It provides a function
elevate which might be what you are looking for.Andrew Kostenko
Is there are any way to pass a relative path to powershell script as second parameter?
Ramyachinna
How to pass arguments for the script file from nodejs
mklement0
This is great in principle, but won't work with scripts with paths that have embedded spaces; it's generally better to invoke scripts via the
-File parameter (and with the -NoProfile parameter for predictability), which also makes it easier to pass arguments: child = spawn("powershell.exe", [ '-noprofile', '-file', "c:\\temp\\helloworld.ps1", "arg1", "arg2"]). Without -File, -Command is implied (in Windows PowerShell, not in PowerShell Core). /cc @Ramyachinna |
The newer way to do this
const { exec } = require('child_process');
exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
// do whatever with stdout
});
4 Comments
Big Money
How can you get this to work with an external powershell script?
Honest Objections
Read the script in as a string and replace 'command here' with the variable
Alwaysa Learner
I looked up for child_process in npm repo and what I found in this link npmjs.com/package/child_process has this message under Read Me section: ** Security holding package - This package name is not currently in use, but was formerly occupied by another package. ** Also it has just 1 version and 7 years old. Just wanted to make sure I got the right one
Johan Dahl
@AlwaysaLearner child_process is a built in package. nodejs.org/docs/latest-v18.x/api/child_process.html
This option works for me, when the script is not already there, but you want to generate some commands dynamically, send them, and work with the results back on node.
var PSRunner = {
send: function(commands) {
var self = this;
var results = [];
var spawn = require("child_process").spawn;
var child = spawn("powershell.exe", ["-Command", "-"]);
child.stdout.on("data", function(data) {
self.out.push(data.toString());
});
child.stderr.on("data", function(data) {
self.err.push(data.toString());
});
commands.forEach(function(cmd){
self.out = [];
self.err = [];
child.stdin.write(cmd+ '\n');
results.push({command: cmd, output: self.out, errors: self.err});
});
child.stdin.end();
return results;
},
};
module.exports = PSRunner;
var spawn = require("child_process").spawn;
let child = spawn("powershell.exe", ["c:/temp/helloworld.ps1"], { shell: true });
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();
NOTE: In Windows, Powershell script can be added irrespective of the full path based on path which you are running this from.