Link to home
Start Free TrialLog in
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?
Avatar of Shabarinath TR
Shabarinath TR
Flag of India image

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
Avatar of oBdA
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

Avatar of 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 ?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial