Determine what OU a Computer Account is in with PowerShell

You’ve been asked to send an email to someone with the location in Active Directory (what Organization Unit or OU) where a particular computer account is located.

As you could have probably guessed, the solution is to query Active Directory with PowerShell and use the Send-MailMessage cmdlet to send the results to the recipient:

I thought I would elaborate a little more based on Jeff Hicks comments. Unless this PowerShell command is being run on a domain controller, the Remote Server Administration Tools would need to be loaded on the machine that this command is being run from. That’s how you get the Microsoft Active Directory PowerShell cmdlets. If you were running PowerShell version 2, you would also have to import the ActiveDirectory PowerShell module before using those cmdlets. Here’s an updated version of the same command that uses a better property which gives the location of a computer account in Active Directory from what I’ll call the top down view instead of the bottom up:

If you don’t know who Jeff Hicks is, you should definitely check out his blog. It’s an honor to have someone like him critique my scripts. I consider Jeff a friend and a mentor. You should also check out his PowerShell Challenge on TrainSignal’s blog this week.



  1. Jeffery Hicks (@JeffHicks)

    You might want to explain where Get-ADComputer comes from and what steps you might need to take first before you can even run this command. And to be technically nitpicky what you are really sending is the full distinguished name, not just the OU name. Still, I get where you’re going with this and it certainly demonstrates how you can achieve a lot with a relatively simple command.

  2. Jeffery Hicks (@JeffHicks)

    You are too kind Mike. Great explanation. And someone really wanted this?

  3. Joe Glim

    This script is what I am looking for – with one suggestion which I’m trying to get to work. I’d like to be able to read in a text file which contains just computer names (one name per line), and then to output a file with the canonical name. Emailing the file is extra, but not necessary.


Leave a Reply

%d bloggers like this: