Calling PowerShell from NodeJS

Question:

I’m attempting to get the owner of a file in Node.js on Windows. In the absence of a win32api, I thought I’d use a PowerShell command:

This works perfectly from the command-line and from a batch file, but just hangs with Node.js exec():

The PowerShell process just appears to start and never terminate.

Does anybody have:

  1. an idea on why the command won’t return in Node.js, or preferably
  2. a sane way for me to get a file owner with Node.js on Windows?

Answer:

When you are calling Powershell like that you need to the close the input stream. You may want to try using spawn and use stdin.end().

Other option is to call cmd /c dir /q <file> but that output is verbose.

Source:

Calling PowerShell from NodeJS by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply