Wednesday, December 17, 2014

Get Multiple Users SamAccount from a Security Group of Active Directory using Powershell

Hi..Today will discuss about small powershell command with that we will get multiple user's Samaccount (Accountname/Logonname/Useraccount), other attributes.

Open your powershell ( windows 2008 server, R2 etc..)

Start -> RUN -> type powershell then type below command to Load the module.

Import-Module ActiveDirectory

or you can directly open the from Start -> Administrative Tools -> then click on the Active Directory Module
for Windows PowerShell  this is will load the Module and Powershell command propmt will open.

Now type below command to get the SamAccount of a Sercurity group

Get-ADGroupmember -Identity "Distinguished Name of the Group" | export-csv -path c:\users\XXXX\desktop\filename.csv -notypeinfo
Example:: domain name is bbntechinfo.com
Group name is : DBA Users
Ou name is : Health
So Distinuguished name of the group is :   "CN=DBA Users,OU=Health,DC=bbntechinfo,DC=com"
if you want to see the Distinuguished name of group

Go to

Start -> Administrative Tools -> Active Directory Users and Computers

Click on Action -> Find   now put the group name and click on Find now once you found the group in search results

right click on the group and properties then click on object you will see the path (Canonical name of object) of the group now go to that path and find the
group now right click  then again click on Attribute Editor in the list of attributes you will see distinguishedName select and click on edit button below
you will see the distinguishedname of that group as Value.

command would be

Get-ADGroupmember -Identity "CN=DBA Users,OU=Health,DC=bbntechinfo,DC=com" | export-csv -path c:\users\XXXX\desktop\filename.csv -notypeinfo

Once you hit enter you will get the file on your desktop with name filename.csv path your wish you can where ever you want to save the file.
now open the file you will see below columns.

Make sure that you should be a domain Admin group of that domain other wise you will not able to run the command, or run as Administrator.

distinguishedName, Name, objectClass, ObjectGUID, SamAccountName  and SID. keep what ever you want and remove other columans.

Let me know if you face any issue with above command. 

No comments:

Post a Comment