Windows PowerShell

Play with node-powershell!


One of my colleague asked me a PowerShell script to query AD User information! Yes, I did your voice!

Import-Module ActiveDirectory 
Get-ADUser -Identity <samaccountname>

But, he asked “How will I do it in NODE JS with no AD Module?” Well! it’s too deep to discuss about node js! Anyways to make it simple I shared an app whichย  serves as a REST endpoint to query AD User information from any platforms like ASP.NET , NODE JS, PowerShell etc.


var express = require('express');
var shell = require('node-powershell');

var app = express();

var ps = new shell({
    executionPolicy: 'bypass',
    noProfile: true

app.get("/:guid", function (req, res) {
    ps.addCommand("./user.ps1", [ {
        name: 'guid', value: req.params.guid
    } ])
    ps.invoke().then(output => {

var server = app.listen(8081, function () {

    var host = server.address().address
    var port = server.address().port
    console.log("Example app listening at http://%s:%s", host, port)


The PS script for User.PS1 is using ADSI ๐Ÿ™‚ with a parameter for samaccountname which is named as GUID (A Friendly Name ๐Ÿ™‚ )!

param (
    [Parameter(Mandatory = $true)]
try {
    $adsi = New-Object adsisearcher 
    $propstoLoad = @("givenname" , "sn" , "samaccountname" , "mail");
    $adsi.Filter = "(&(ObjectCategory=User)(samaccountname=$guid))"
    $result = $adsi.FindOne().Properties
        GivenName = $result['givenname'] -as [string]
        SurName   = $result['sn'] -as [string]
        GUID      = $result['samaccountname'] -as [string]
        mail      = $result['mail'] -as [string]
    } | ConvertTo-Json
catch {

Spin your NODE and browse for the result!ย  REST URL :

    "GivenName":  "Chendrayan",
    "SurName":  "Venkatesan",
    "GUID":  "ChenV",
    "mail":  ""

The RESTful continues! Happy PowerShell!

Leave a Reply

Your email address will not be published. Required fields are marked *