Determine what OU a Computer Account is in with PowerShell

Problem:
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.

Solution:
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:

Update:
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.

µ

2 Comments

  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.

    Reply
  2. Jeffery Hicks (@JeffHicks)

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

    Reply

Leave a Reply

%d bloggers like this: