Avatar of tonelm54
tonelm54
 asked on

Get AD OU Path

Is there a command I can issue on a clients PC which will show where in AD the PC is?

For example, if I ran it on my PC, it should return like:-
dom1\Company1\UK\West Midlands\BU006\Laptops.

Any suggestions?
PowershellActive Directory

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
Shabarinath TR

Get-ADComputer "ComputerName" -Properties * |Select CanonicalName

You need Active Directory module installed .

Refer how to install RSAT for AD Powershell module - https://theitbros.com/install-and-import-powershell-active-directory-module/

Cheers
oBdA

This will return any computer's CanonicalName (and default to the local computer).
Plain ADSI, will run on any Windows machine:
Function Get-AdsiComputerCN([Parameter(ValueFromPipeline=$true)][String]$ComputerName = $env:ComputerName) {
	Process {
		$adsiSearcher = [adsisearcher]"(&(objectCategory=Computer)(name=$($ComputerName)))"
		[void]$adsiSearcher.PropertiesToLoad.Add('canonicalname')
		$adsiSearcher.FindOne().Properties.canonicalname
		$adsiSearcher.Dispose()
	}
}

Open in new window

tonelm54

ASKER
Im liking oBdA solution as dont need to install anything, but how do I call it? In Powershell and just call the function Get-AdsiComputerCN ?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.