1

I need to read some data using Powershell using Node.js. I tried several different packages to use the powershell command in Node.js and get data from the console, but every time I get data from Powershell the data is trimmed.

For example, my command is:

Get-ChildItem -Exclude *procesy | sort CreationTime -desc

The correct results in the Powershell window are:

PS C:\Data> Get-ChildItem -Exclude *procesy | sort CreationTime -desc                  

Directory: C:\Users\Misiek\Desktop\Skrypty\!Backup


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        01.01.2021     03:22            966 01.01.2021 03-22-52 - katalogi Explorer.txt
-a----        01.01.2021     03:22           5784 01.01.2021 03-22-52 - otwarte procesy.txt
-a----        01.01.2021     03:32           1010 01.01.2021 03-32-54 - katalogi Explorer.txt
-a----        01.01.2021     03:32           5478 01.01.2021 03-32-54 - otwarte procesy.txt
-a----        01.01.2021     03:42           1064 01.01.2021 03-42-55 - katalogi Explorer.txt
-a----        01.01.2021     03:42           5820 01.01.2021 03-42-55 - otwarte procesy.txt
-a----        01.01.2021     03:52           1064 01.01.2021 03-52-57 - katalogi Explorer.txt
-a----        01.01.2021     03:52           5460 01.01.2021 03-52-57 - otwarte procesy.txt
-a----        01.01.2021     04:02           1064 01.01.2021 04-02-58 - katalogi Explorer.txt
-a----        01.01.2021     04:02           5462 01.01.2021 04-02-58 - otwarte procesy.txt
-a----        01.01.2021     04:13           1064 01.01.2021 04-13-00 - katalogi Explorer.txt
-a----        01.01.2021     04:13           5464 01.01.2021 04-13-00 - otwarte procesy.txt
-a----        01.01.2021     04:23           1064 01.01.2021 04-23-01 - katalogi Explorer.txt
-a----        01.01.2021     04:23           5462 01.01.2021 04-23-01 - otwarte procesy.txt
-a----        01.01.2021     04:33           1064 01.01.2021 04-33-03 - katalogi Explorer.txt
-a----        01.01.2021     04:33           5462 01.01.2021 04-33-03 - otwarte procesy.txt
-a----        01.01.2021     04:43           1064 01.01.2021 04-43-05 - katalogi Explorer.txt
-a----        01.01.2021     04:43           5462 01.01.2021 04-43-05 - otwarte procesy.txt

But I get the trimmed results in Node.js console window:

C:\!Temp\ExplorerFolderManager>npm run start

> [email protected] start C:\!Temp\ExplorerFolderManager
> electron .



    Directory: C:\Users\Misiek\Desktop\Skr
    ypty\!Backup


Mode                 LastWriteTime  Length
----                 -------------  ------
-a----        01.01.2021     03:22     966
-a----        01.01.2021     03:22    5784
-a----        01.01.2021     03:32    1010
-a----        01.01.2021     03:32    5478
-a----        01.01.2021     03:42    1064
-a----        01.01.2021     03:42    5820
-a----        01.01.2021     03:52    1064
-a----        01.01.2021     03:52    5460
-a----        01.01.2021     04:02    1064
-a----        01.01.2021     04:02    5462
-a----        01.01.2021     04:13    1064
-a----        01.01.2021     04:13    5464
-a----        01.01.2021     04:23    1064
-a----        01.01.2021     04:23    5462
-a----        01.01.2021     04:33    1064
-a----        01.01.2021     04:33    5462
-a----        01.01.2021     04:43    1064
-a----        01.01.2021     04:43    5462

I tried a node-cmd package (trimmed data from powershell console). With child_process I get trimmed data also:

const xxxx = spawn('powershell.exe', ['Get-ChildItem', '-Path', dir], {
});

xxxx.stdout.on('data', (data) => {
    onePrintStringData = data.toString();
  console.log(onePrintStringData);
});

or

exec('powershell "Get-ChildItem -Path ' + dir + ' -Exclude *procesy.txt"', (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

Using the node-powershell package this same - trimmed data :/

const ps = new Shell({
  executionPolicy: 'Bypass',
  noProfile: true
});

ps.addCommand('powershell "Get-ChildItem -Path ' + dir + ' -Exclude *procesy.txt"');
ps.invoke()
.then(output => {
  console.log(output);
})
.catch(err => {
  console.log(err);
});
5
  • 1
    Why are you calling a powershell command from JS? Why not either write the entire thing in powershell script (or the entire thing in JS)? Commented Jan 4, 2021 at 11:18
  • @Olian04 I would like to use a Powershell because I already have a command to get files with sorting using modify date. Using Node.js it is some harder to do :/ Commented Jan 4, 2021 at 11:35
  • 2
    Powershell itself produces funky results when outputting table-formatted data into a shell window based on how the table formatting is configured, and the window's columns (width). Likley node.js is instantiating a window with reduced size, and powershell is behaving badly, or node.js is not playing well with how powershell shifts the data. Writing this only in PS, or only in node.js would likely yield better results Commented Jan 4, 2021 at 12:01
  • Alternatively, if you really want to do ps to node.js, you could use the convertto-json cmdlet to convert the object to the json format and have node.js read that in from powershell. Something like (Get-ChildItem -Exclude *procesy | sort CreationTime -desc) | ConvertTo-json. Alternatively, you could have powershell use list format, and painfully scrape that content into a table of your own in node.js, ofc. Commented Jan 4, 2021 at 12:17
  • Thanks guys, I chose the JS way :) Commented Jan 4, 2021 at 13:45

1 Answer 1

1

I would like to use a Powershell because I already have a command to get files with sorting using modify date. Using Node.js it is some harder to do :/

It doesn't have to be harder. You can fetch the filenames using fs.readdir, then fetch the modification date using fs.stat, then sort on the modification date, then extract the filenames, like this:

const fs = require('fs');
const path = require('path');

const dir = '.';

const files = fs.readdirSync(dir)
  .map(fileName => ({
    name: fileName,
    time: fs.statSync(path.join(dir, fileName)).mtime.getTime()
  }))
  .sort((a, b) => a.time - b.time)
  .map(v => v.name);

console.log(files);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Everything works now awesome :D It is better way to use it only with JavaScript ;)
@Michal that's great. Please feel free to accept my answer by pressing the Checkmark next to the upvote arrows. That way other users will see that the question has been satisfactorily answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.