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?
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.

AlanConsultantCommented:
Hi,

You can use this to return the AD domain name:

(Get-WmiObject Win32_ComputerSystem).Domain

Open in new window



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

CN=ExampleComputerame,OU=ExampleADOU,DC=ADDomainName1,DC=ADDomainName2

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'.


Alan.
0
What were the top attacks of Q1 2018?

The Threat Lab team analyzes data from WatchGuard’s Firebox Feed, internal and partner threat intelligence, and a research honeynet, to provide insightful analysis about the top threats on the Internet. Check out our Q1 2018 report for smart, practical security advice today!

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

But, I need just the "domain.local" or just "domain"
0
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 $computer.name -Class Win32_computerSystem).Domain}

Open in new window

1
oBdACommented:
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

0

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
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.