├── .github └── FUNDING.yml ├── .gitignore ├── CONTRIBUTING.md ├── DOCUMENTATION.md ├── LICENSE ├── README.md ├── example ├── PSCore.js └── index.js ├── lib └── index.js └── package.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: ionicabizau 2 | patreon: ionicabizau 3 | open_collective: ionicabizau 4 | custom: https://www.buymeacoffee.com/h96wwchmy -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.swo 3 | *~ 4 | *.log 5 | node_modules 6 | *.env 7 | .DS_Store 8 | package-lock.json 9 | .bloggify/* 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # 🌟 Contributing 2 | 3 | Want to contribute to this project? Great! Please read these quick steps to streamline the process and avoid unnecessary tasks. ✨ 4 | 5 | ## 💬 Discuss Changes 6 | Start by opening an issue in the repository using the [bug tracker][1]. Describe your proposed contribution or the bug you've found. If relevant, include platform info and screenshots. 🖼️ 7 | 8 | Wait for feedback before proceeding unless the fix is straightforward, like a typo. 📝 9 | 10 | ## 🔧 Fixing Issues 11 | 12 | Fork the project and create a branch for your fix, naming it `some-great-feature` or `some-issue-fix`. Commit changes while following the [code style][2]. If the project has tests, add one. ✅ 13 | 14 | If a `package.json` or `bower.json` exists, add yourself to the `contributors` array; create it if it doesn't. 🙌 15 | 16 | ```json 17 | { 18 | "contributors": [ 19 | "Your Name (http://your.website)" 20 | ] 21 | } 22 | ``` 23 | 24 | ## 📬 Creating a Pull Request 25 | Open a pull request and reference the initial issue (e.g., *fixes #*). Provide a clear title and consider adding visual aids for clarity. 📊 26 | 27 | ## ⏳ Wait for Feedback 28 | Your contributions will be reviewed. If feedback is given, update your branch as needed, and the pull request will auto-update. 🔄 29 | 30 | ## 🎉 Everyone Is Happy! 31 | Your contributions will be merged, and everyone will appreciate your effort! 😄❤️ 32 | 33 | Thanks! 🤩 34 | 35 | [1]: /issues 36 | [2]: https://github.com/IonicaBizau/code-style -------------------------------------------------------------------------------- /DOCUMENTATION.md: -------------------------------------------------------------------------------- 1 | ## Documentation 2 | 3 | You can see below the API reference of this module. 4 | 5 | ### `PowerShell(input, opt, cb)` 6 | 7 | #### Params 8 | 9 | - **String** `input`: The command or PowerShell script ro execute. 10 | - **Object** `opt`: An object containing the following fields: 11 | - `debug` (Boolean): Turn on/off the debug messages (default: `false`). 12 | - `noprofile` (Boolean): Turn on/off noprofile parameter (default: `true`). 13 | - `executionpolicy` (Enum): Run powershell with specified executionpolicy (default: System default). Valid enum values are `Restricted`, `AllSigned`, `RemoteSigned`, `Unrestricted`, `Bypass`, `Undefined`. 14 | - `PSCore` (Boolean) : Turn on/off 'pwsh' the executable for PowerShell Core as opposed to Windowes PowerShell (default: 'false'). 15 | - **Function** `cb`: The callback function (optional). 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-25 Ionică Bizău (https://ionicabizau.net) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | # powershell 21 | 22 | [![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [![Ask me anything](https://img.shields.io/badge/ask%20me-anything-1abc9c.svg)](https://github.com/IonicaBizau/ama) [![Version](https://img.shields.io/npm/v/powershell.svg)](https://www.npmjs.com/package/powershell) [![Downloads](https://img.shields.io/npm/dt/powershell.svg)](https://www.npmjs.com/package/powershell) [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/@johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github) 23 | 24 | Buy Me A Coffee 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | > Run PowerShell scripts and commands from Node.js. 33 | 34 | 35 | 36 | 37 | 38 | 39 | ## Why? 40 | 41 | 42 | - Actively maintained 43 | - Improved codebase. 44 | - Written in ES2015 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | ## :cloud: Installation 59 | 60 | ```sh 61 | # Using npm 62 | npm install --save powershell 63 | 64 | # Using yarn 65 | yarn add powershell 66 | ``` 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | ## :clipboard: Example 81 | 82 | 83 | 84 | ```js 85 | const PowerShell = require("powershell"); 86 | 87 | // Start the process 88 | let ps = new PowerShell("echo 'powershell is awesome'"); 89 | 90 | // Handle process errors (e.g. powershell not found) 91 | ps.on("error", err => { 92 | console.error(err); 93 | }); 94 | 95 | // Stdout 96 | ps.on("output", data => { 97 | console.log(data); 98 | }); 99 | 100 | // Stderr 101 | ps.on("error-output", data => { 102 | console.error(data); 103 | }); 104 | 105 | // End 106 | ps.on("end", code => { 107 | // Do Something on end 108 | }); 109 | ``` 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | ## :question: Get Help 123 | 124 | There are few ways to get help: 125 | 126 | 127 | 128 | 1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question. 129 | 2. For bug reports and feature requests, open issues. :bug: 130 | 3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket: 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | ## :memo: Documentation 139 | 140 | 141 | ### `PowerShell(input, opt, cb)` 142 | 143 | #### Params 144 | 145 | - **String** `input`: The command or PowerShell script ro execute. 146 | - **Object** `opt`: An object containing the following fields: 147 | - `debug` (Boolean): Turn on/off the debug messages (default: `false`). 148 | - `noprofile` (Boolean): Turn on/off noprofile parameter (default: `true`). 149 | - `executionpolicy` (Enum): Run powershell with specified executionpolicy (default: System default). Valid enum values are `Restricted`, `AllSigned`, `RemoteSigned`, `Unrestricted`, `Bypass`, `Undefined`. 150 | - `PSCore` (Boolean) : Turn on/off 'pwsh' the executable for PowerShell Core as opposed to Windowes PowerShell (default: 'false'). 151 | - **Function** `cb`: The callback function (optional). 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | ## :yum: How to contribute 167 | Have an idea? Found a bug? See [how to contribute][contributing]. 168 | 169 | 170 | ## :sparkling_heart: Support my projects 171 | I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, 172 | this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it). 173 | 174 | However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it: 175 | 176 | 177 | - Starring and sharing the projects you like :rocket: 178 | - [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you after years if you buy me one. :grin: :book: 179 | - [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea: 180 | - [![Support me on Patreon][badge_patreon]][patreon]—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone). 181 | - **Bitcoin**—You can send me bitcoins at this address (or scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6` 182 | 183 | ![](https://i.imgur.com/z6OQI95.png) 184 | 185 | 186 | Thanks! :heart: 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | ## :cake: Thanks 197 | This module is heavily based on [`node-powershell`](https://github.com/rannn505/node-powershell) by [@rann505](https://github.com/rannn505/). 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | ## :scroll: License 215 | 216 | [MIT][license] © [Ionică Bizău][website] 217 | 218 | 219 | 220 | 221 | 222 | 223 | [license]: /LICENSE 224 | [website]: https://ionicabizau.net 225 | [contributing]: /CONTRIBUTING.md 226 | [docs]: /DOCUMENTATION.md 227 | [badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg 228 | [badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg 229 | [badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg 230 | [badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg 231 | [patreon]: https://www.patreon.com/ionicabizau 232 | [amazon]: http://amzn.eu/hRo9sIZ 233 | [paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW 234 | -------------------------------------------------------------------------------- /example/PSCore.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const PowerShell = require("../lib"); 4 | 5 | // Start the process 6 | let ps = new PowerShell("echo 'powershell is awesome'",{PSCore:true}); 7 | 8 | // Handle process errors (e.g. powershell not found) 9 | ps.on("error", err => { 10 | console.error(err); 11 | }); 12 | 13 | // Stdout 14 | ps.on("output", data => { 15 | console.log(data); 16 | }); 17 | 18 | // Stderr 19 | ps.on("error-output", data => { 20 | console.error(data); 21 | }); 22 | 23 | // End 24 | ps.on("end", code => { 25 | // Do Something on end 26 | }); 27 | -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const PowerShell = require("../lib"); 4 | 5 | // Start the process 6 | let ps = new PowerShell("echo 'powershell is awesome'"); 7 | 8 | // Handle process errors (e.g. powershell not found) 9 | ps.on("error", err => { 10 | console.error(err); 11 | }); 12 | 13 | // Stdout 14 | ps.on("output", data => { 15 | console.log(data); 16 | }); 17 | 18 | // Stderr 19 | ps.on("error-output", data => { 20 | console.error(data); 21 | }); 22 | 23 | // End 24 | ps.on("end", code => { 25 | // Do Something on end 26 | }); 27 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const isUndefined = require("is-undefined") 4 | , EventEmitter = require("events").EventEmitter 5 | , spawn = require("spawno") 6 | , isWin = require("is-win") 7 | ; 8 | 9 | module.exports = class PowerShell extends EventEmitter { 10 | /** 11 | * PowerShell 12 | * 13 | * @name PowerShell 14 | * @function 15 | * @param {String} input The command or PowerShell script ro execute. 16 | * @param {Object} opt An object containing the following fields: 17 | * 18 | * - `debug` (Boolean): Turn on/off the debug messages (default: `false`). 19 | * - `noprofile` (Boolean): Turn on/off noprofile parameter (default: `true`). 20 | * - `executionpolicy` (Enum): Run powershell with specified executionpolicy (default: System default). Valid enum values are `Restricted`, `AllSigned`, `RemoteSigned`, `Unrestricted`, `Bypass`, `Undefined`. 21 | * - `PSCore` (Boolean) : Turn on/off 'pwsh' the executable for PowerShell Core as opposed to Windowes PowerShell (default: 'false'). 22 | * 23 | * @param {Function} cb The callback function (optional). 24 | */ 25 | constructor (input, opt, cb){ 26 | super(); 27 | 28 | opt = opt || {}; 29 | opt.debug = isUndefined(opt.debug) ? false : opt.debug; 30 | opt.noprofile = isUndefined(opt.noprofile) ? true : opt.noprofile; 31 | opt.PSCore = isUndefined(opt.PSCore) ? false : opt.PSCore; 32 | const EXE_NAME = `${opt.PSCore?'pwsh':'powershell'}${isWin() ? ".exe" : ""}`; 33 | let args = []; 34 | 35 | if (opt.noprofile) { 36 | args.push("-NoProfile"); 37 | } 38 | 39 | if (!isUndefined(opt.executionpolicy)) { 40 | args.push("-ExecutionPolicy", opt.executionpolicy); 41 | } 42 | 43 | args.push("-Command", "& {" + input + "}"); 44 | 45 | let _proc = this.proc = spawn( 46 | EXE_NAME 47 | , args 48 | , { stdio: ["ignore", "pipe", "pipe" ] } 49 | , cb 50 | ); 51 | 52 | _proc.stdout.setEncoding("utf8"); 53 | _proc.stderr.setEncoding("utf8"); 54 | _proc.on("error", err => this.emit("error", err)); 55 | 56 | if (opt.debug){ 57 | console.log(`<${EXE_NAME}> Starting ${_proc.pid} on ${process.platform}`); 58 | } 59 | 60 | let chunks = []; 61 | 62 | _proc.stdout.on("data", chunk => { 63 | if (typeof chunk === "string") { 64 | chunks.push(chunk); 65 | } else { 66 | chunks.push.apply(chunks, chunk); 67 | } 68 | }); 69 | 70 | _proc.stderr.on("data", err => this.emit("error-output", err)); 71 | _proc.on("close", code => { 72 | if (opt.debug) { 73 | console.log(`<${EXE_NAME}> Process ${_proc.pid} exited with code ${code}`); 74 | } 75 | this.emit("output", chunks.join("")) 76 | this.emit("end", code); 77 | }); 78 | } 79 | }; 80 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "powershell", 3 | "version": "2.3.3", 4 | "description": "Run PowerShell scripts and commands from Node.js.", 5 | "main": "lib/index.js", 6 | "directories": { 7 | "example": "example" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+ssh://git@github.com/IonicaBizau/powershell.git" 15 | }, 16 | "keywords": [ 17 | "powershell", 18 | "commands", 19 | "scripts" 20 | ], 21 | "author": "Ionică Bizău (https://ionicabizau.net)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/IonicaBizau/powershell/issues" 25 | }, 26 | "homepage": "https://github.com/IonicaBizau/powershell#readme", 27 | "blah": { 28 | "description": [ 29 | { 30 | "h2": "Why?" 31 | }, 32 | { 33 | "ul": [ 34 | "Actively maintained", 35 | "Improved codebase.", 36 | "Written in ES2015" 37 | ] 38 | } 39 | ], 40 | "thanks": "This module is heavily based on [`node-powershell`](https://github.com/rannn505/node-powershell) by [@rann505](https://github.com/rannn505/)." 41 | }, 42 | "dependencies": { 43 | "is-undefined": "^1.0.0", 44 | "is-win": "^1.0.2", 45 | "spawno": "^1.0.0" 46 | }, 47 | "files": [ 48 | "bin/", 49 | "app/", 50 | "lib/", 51 | "dist/", 52 | "src/", 53 | "scripts/", 54 | "resources/", 55 | "menu/", 56 | "cli.js", 57 | "index.js", 58 | "index.d.ts", 59 | "package-lock.json", 60 | "bloggify.js", 61 | "bloggify.json", 62 | "bloggify/" 63 | ] 64 | } --------------------------------------------------------------------------------