# Simple usage

## 1. Logging PowerShell version

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

{% code title="simple-example.js" %}
```javascript
// 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 + "!");
});
```
{% endcode %}

#### PowerShell Version Table:

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

<figure><img src="https://2193159300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8SiNigX5IIotGXFYF2oe%2Fuploads%2Fl67tT7eYGVcFBT5Levfw%2Fimage.png?alt=media&#x26;token=e2b1ffa9-4332-4194-907c-ae606b0ae7fb" alt=""><figcaption><p>Execution result</p></figcaption></figure>

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:

<figure><img src="https://2193159300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8SiNigX5IIotGXFYF2oe%2Fuploads%2FsWIRDDlMyB65tdpQ5fUB%2Fimage.png?alt=media&#x26;token=7a384f65-26c2-4068-9796-820c67c7dbe8" alt=""><figcaption><p>Execution result</p></figcaption></figure>

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 destructure[^1] 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](#powershell-version-table "mention"), 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

{% code title="super-simple-example.js" %}
```javascript
// 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();
});
```
{% endcode %}

<figure><img src="https://2193159300-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8SiNigX5IIotGXFYF2oe%2Fuploads%2Fo0MlBbiZm7fBgUQ7k8UW%2Fimage.png?alt=media&#x26;token=4c7c8428-eaea-499b-8b0b-a422af414628" alt=""><figcaption><p>Execution result</p></figcaption></figure>

## 3.

[^1]: [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring\_assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)
