The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 717 other subscribers

batch files: getting information from your Windows AD

Posted by jpluimers on 2011/10/13

If you have the Windows Server Resource Kit tools installed, then dsget and dsquery can get you some valuable information about the current user and computer.

Below is a sample batch file that shows that dsquery gets you [Wayback/Archive] DNs, and dsget gets you detailed information for a certain type of DN.

You can use pipes (and sometimes you need to [Wayback/Archive] escape the pipes) to pass information from dsquery to dsget.
dsget will happily accept multiple DNs (each on a new line), so you can use text-files with DNs too.

[sourcecode language=”powershell”]
@echo on
:: this assumes that dsquery, dsget, etc are in the same directory as the batch file
::escape pipe with caret
for /f "tokens=* delims= " %%a in (‘%~dp0dsquery user -samid %USERNAME% ^| %~dp0dsget user -desc ^| find /V "dsget succeeded"’) do (
set description=%%a
)
::trim last two spaces
if "%description:~-2%"==" " set description=%description:~0,-2%
echo !%description%!
[/sourcecode]

–jeroen

One Response to “batch files: getting information from your Windows AD”

  1. […] second batch file escapes the pipe (|) by using a carret (^), so it is passed from the command-line to […]

Leave a Comment