Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Check user exist in AD with powershell

avanis
Kilo Contributor

I have a condition to check if user exist in AD so that while rehiring we do not create a new userid for the same user who has a deactivated account and reactivate the same account.

I found ways to to do that via AD activities but that uses LDAP.

I do not have LDAP and want to do the same via powershell script.

Can anyone help how to do that.

13 REPLIES 13

adilrathore
ServiceNow Employee
ServiceNow Employee

I think as a pre-requisite you need to have RSAT Installed on a machine from where you would like to query AD.


We have it but i need powershell script and not use ad activities.


adilrathore
ServiceNow Employee
ServiceNow Employee

Here is an example:



Import-Module Activedirectory



$newusers = @("john.snow","ned.stark01","tyron.lannister")



foreach ($newuser in $newusers)


{


if (Get-aduser $newuser)


    {


    write-host "$newuser already exists"


    }


else


    {


    New-aduser -samaccountname $newuser


    }


}



For more information consult this page:



[SOLVED] Best Way to Check if Username Exists and Write the Username to File - PowerShell - Spicewor...


avanis please mark the reply as Answered.