Simple usage

1. Logging PowerShell version

This example is the most popular thing that people love about using PowerShell

simple-example.js
// Print PowerShell Version using PowerJS
import { PowerJS } from "@obayd/powerjs";

const instance = new PowerJS(/* options */);

instance.exec("$PSVersionTable").then((result) => {
  // You may notice some slowdown, that's because of the instance init process.
  // After the instance is started, you can enjoy a blazing fast environnement!
  console.log("Currently on Powershell v" + result.PSVersion.Major + "!");
});

PowerShell Version Table:

Try executing $PSVersionTable and $PSVersionTable.PSVersion in a PowerShell terminal.

You should see something simillar to the above.

PowerJS behind the scenes try to transport data between PowerShell and your Node Process

When running the example code, you should see:

However, the 7 there is the same as $PSVersionTable.PSVersion.Major, that means it can differ in your side.

In step-by-step:

  1. Import PowerJS library using the import statement. Currently PowerJS exports many things, so you will need to imported object using {...} ,and get only PowerJS object

  2. Create an instance using the new operator.

  3. Execute $PSVersionTable to get A version table object PowerShell Version Table:, then wait for response.

  4. Extract the Major part of the version, using result.PSVersion.Major.

2. Logging User names

This task is common when it comes to automation and system management

super-simple-example.js
// Read the local user list
import { PowerJS } from "@obayd/powerjs";

const instance = new PowerJS();

instance.exec(",(Get-LocalUser)").then(function (result) {
  // Use the , to make arrays returnable, otherwise, it will return only the first item
  // Read https://stackoverflow.com/questions/29973212/pipe-complete-array-objects-instead-of-array-items-one-at-a-time
  
  for (const user of result) {
    console.log(user.Name);
  }
  process.exit();
});

3.

Last updated