How to list domain name from Active Directory with powershell

Hello, I have the following code and i would like to capture the domain name, i can not find it through properties though.
Get-ADComputer -Filter * -Properties * | Where-Object {$_.OperatingSystem -like "Windows Server*"} | Select name,DNSHostName,IPv4Address,CanonicalName,OperatingSystemVersion

Open in new window

Thanks for your help,
namergSystems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


You can use this to return the AD domain name:

(Get-WmiObject Win32_ComputerSystem).Domain

Open in new window

namergSystems AdministratorAuthor Commented:
This (Get-WmiObject Win32_ComputerSystem).Domain works only for one computer not multiple computers grabbed from AD through my code.
The 'DistinguishedName' property should include the AD domain name:


So maybe this:

Get-ADComputer -Filter * -Properties * | Where-Object {$_.OperatingSystem -like "Windows Server*"} | Select DistinguishedName,Name,DNSHostName,IPv4Address,CanonicalName,OperatingSystemVersion

Open in new window

You might not need 'Name' if you are pulling 'DistinguishedName'.

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

namergSystems AdministratorAuthor Commented:
Correct. CN=HOSTNAME,OU=Domain Controllers,DC=domain,DC=local

But, I need just the "domain.local" or just "domain"
David Johnson, CD, MVPOwnerCommented:
get-adcomputer with no parameters will only return computers in the CURRENTLY logged on Domain.

$computers = Get-ADComputer -Filter * -Properties * | Where-Object {$_.OperatingSystem -like "Windows Server*"} | Select name,DNSHostName,IPv4Address,CanonicalName,OperatingSystemVersion
foreach($computer in $computers){ (Get-WmiObject -ComputerName $ -Class Win32_computerSystem).Domain}

Open in new window

You have it as part of DNSHostName, and you can add it to your results using a calculated property:
Get-ADComputer -Filter * -Properties * | Where-Object {$_.OperatingSystem -like "Windows Server*"} | Select Name, DNSHostName, IPv4Address, CanonicalName, OperatingSystemVersion, @{n='DNSDomain'; e={$_.DNSHostName.Split('.', 2)[1]}}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
namergSystems AdministratorAuthor Commented:
Thanks oBdA
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.