Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can DSQUERY commands be piped together?

eg to return a list of users in each of the groups found within the 'Builtin' container?

dsquery groups "cn=Builtin,dc=domain,dc=company" | dsquery groups "cn=%builtingroups%cn=Builtin,dc=domain,dc=company" -members >>usergroups.txt

Where %builtingroups% is the result of the first query (ie a list of groups)


share|improve this question
I think this question is similar but doesn't return the result i'm looking for (ie a user list within every group contained per container) – HaydnWVN Jan 9 '12 at 12:44
up vote 2 down vote accepted
for /f "usebackq delims=" %a in (`dsquery group "cn=Builtin,dc=domain,dc=company"`) do echo ===%a members: >> members.txt && dsget group %a -members >> members.txt
share|improve this answer
thanks, i'll try it later! Care to explain what each part does? I can partly figure it out myself... :) – HaydnWVN Jan 10 '12 at 13:10
Well, it gets list of groups with "dsquery group", then it goes through that list and for each line calls dsget to pick members. "echo ... &&" is just to make output more readable so you can omit that. Type "for /?" for usebackq and delims explanation. It will be more clear when you look at it running. – Dusan Bajic Jan 10 '12 at 13:27
Excellent, thanks! :) – HaydnWVN Jan 10 '12 at 13:40
dsquery failed:A referral was returned from the server. Any ideas to troubleshoot? – HaydnWVN Jan 11 '12 at 19:44
try dsquery alone: dsquery group "cn=Builtin,dc=domain,dc=company", it is usually a typing error – Dusan Bajic Jan 12 '12 at 13:24

In my case, the referral was for a query for a user in a Trusting Domain from a Workstation in the Trusted domain. (One way trust Trusting->Trusted)


dsquery user "DC=TRUSTING,DC=DOM" -samid <account in Trusting.dom>
dsquery failed:A referral was returned from the server.

Why? Because the Domain Controllers for are all behind a firewall and unreachable.

Additional diagnostic:

dsquery user "DC=TRUSTING,DC=DOM" -samid <account in Trusting.dom> -s DC1.Trusted.dom
dsquery failed:The server is not operational.

When a VPN from the network for WKS.Trusted.DOM is established to the network for Trusting.DOM is established, the query succeeds just fine.

share|improve this answer
You've posted this as an answer to the original question. Perhaps it is more suited as a comment. – jscott Oct 13 '15 at 19:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.