How to find computer object OU from server itself using CMD
Asked Answered
F

1

6

How can I find what OU the computer belongs to using CMD or powershell cmdlet . I have found some long scripts but there has to be a one liner command for it. The computers dont have AD role installed on them so DSQUERY is out of equation. CMD or powershell cmdlet would work.

Felonry answered 17/3, 2016 at 18:29 Comment(0)
W
11

A one-liner for PowerShell without AD Role would be:

([adsisearcher]"(&(name=$env:computername)(objectClass=computer))").findall().path

That would give you a string like:

LDAP://CN=MyComputer,OU=Workstations,OU=Machines,DC=TacoTruck,DC=com

That would work on the local computer. If you want to run against a remote server on the same domain replace $env:computername with the name of the computer.

Wheedle answered 17/3, 2016 at 18:53 Comment(1)
Add -replace '^.*?(?=OU=)' or access parent (might need to get directoryentry first), and you're done. :-)Heartily

© 2022 - 2024 — McMap. All rights reserved.