I need to know how to get the result in VBA Excel from Active Directory using shell, cmd, dsquery and dsget filtering by email in this way:

---> dsquery user -name m * | dsget user -email

If I write the above commands in a cmd (it doesn't need admin for the query) it returns what I ask for, now if I send it from VBA in Excel with the Shell command like this:

---> R = Shell ("cmd dsquery user -name m * | dsget user -email", vbNormalNoFocus)

a cmd opens but it does nothing else and the most curious thing is that I use those same commands in the cmd opened from VBA Excel then appears the following:

---> 'dsquery' is not recognized as an internal or external command, operable program or batch file

What am I doing wrong? Thanks very much!

  • VBA running an external executable to query AD? You really should revise your approach to this. – Massimo Apr 2 '20 at 16:43
  • I have tried using powershell to do it with the powershell's parameters but when I try the code Excel closes. :o ... I'll continue researching – ttlecaster Apr 3 '20 at 17:52
  • Even if you want to do it from VBA, there are libraries for this. Calling an external program and parsing its output is like emailing a picture by printing it, scanning it and then attaching the scan. – Massimo Apr 3 '20 at 19:01
  • Thanks for your comments.!!! – ttlecaster Apr 5 '20 at 15:11

dsquery is part of an optional feature on Windows workstation. There are also much better ways of doing this than using dsquery.

  • And there are much, much better approaches than have a VBA macro run an external executable... – Massimo Apr 2 '20 at 16:42

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