I had a requirement to get a list of all users in a specific group in active directory running on a Windows server. I needed to pull a list of windows user names back into a shell script on an AIX system in order to set samba file sharing permissions for a specific subset of users.

The best tool for the job is ldapsearch so first you need to install openldap.

Once you have ldapsearch installed you can use the following command to extract the list of users as follows. Please note that ldap_service_user needs to be a valid user on the windows domain who has the ability to query ldap. In my case it was my own windows login account. The command will also prompt you for your windows password.

ldapsearch \
-xLLL -h domain.local \
-D “[email protected]” \
-W \
-b “OU=All Others,OU=Users Accounts,DC=domain,DC=local” \
-s sub -x “(objectclass=user)” memberOf=cn=Domain Admins,ou=All Others,dc=domain,dc=Local sAMAccountName |grep sAM|awk ‘{print $2}’

If you want to hard code the windows password you are using into the command you can do the following

echo -n yourwindowspassword > pass.txt


ldapsearch \
-xLLL -h domain.local \
-D “[email protected]” \
-y pass.txt \
-b “OU=All Others,OU=Users Accounts,DC=domain,DC=local” \
-s sub -x “(objectclass=user)” memberOf=cn=Domain Admins,ou=All Others,dc=domain,dc=Local sAMAccountName |grep sAM|awk ‘{print $2}’

Then run ldapsearch and you will not need to enter a password.