[Webinar] Learn how to a build a cloud-first strategyRegister Now


Batch Script to Get Computer's OU

Posted on 2009-02-20
Medium Priority
Last Modified: 2012-05-06
Can someone tell me how to return the OU a computer is in using batch? When someone logs on to their computer, I want a logon script to run that can determine what OU the computer is. I assume I could run a command that searches Active Directory and pass it %computername%, but I'm not sure how. Also, the OU should be set to a variable like %ou% or something.

Thanks in advance.
Question by:ProUAdmin
  • 2
  • 2
LVL 18

Accepted Solution

Learnctx earned 2000 total points
ID: 23698748
Try this, it 'works'..to a fashion as long as dsquery is available on the machine its running from.
for /f "delims=* usebackq" %a in (`@dsquery computer -name %Computername%`) do @set computer=%a
set ou=%computer:"=%
set ou=%ou:*,OU=OU%
echo %ou%

Open in new window


Author Comment

ID: 23714070
I got the error below. Any thoughts?
computernamea was unexpected at this time.

Open in new window

LVL 18

Assisted Solution

Learnctx earned 2000 total points
ID: 23717110
If you're running it from a batch file you're need to replace %a with %%a.
Also add @ECHO OFF to the top of the batch file to avoid lots of feedback.

Author Comment

ID: 23717719
That worked perfect. Thank you.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here's a look at newsworthy articles and community happenings during the last month.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question