Get ad computers using powershell

Suthar, Anil 21 Reputation points
2021-06-15T05:46:00.277+00:00

I'm using below statement to get server lists from AD using windows server filter. I've two different domain "abc.com" and another "anil.abc.com". I've an access to read information from both the domains, when i run the script i get data only from the "abc.com" , but cannot get the server lists from "anil.abc.com". Can you please help what i can use to get a data from both using a single script ? Thanks in advance.

My script

$ScriptPath = Get-Location
Get-ADComputer -Filter {(OperatingSystem -like "windowsserver*") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt

Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-06-15T06:43:50.117+00:00

    Hi @Suthar, Anil ,
    Let's give it another try please:

     $ScriptPath = Get-Location  
     Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt  
     Get-ADComputer -Server <yourDC of anil.abc.com> -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt -Append  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-06-15T05:56:37.107+00:00

    Hi @Suthar, Anil ,

    please give this a try:

    $ScriptPath = Get-Location  
    Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt  
    Get-ADComputer -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=anil,DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt -Append  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-06-15T06:31:30.023+00:00

    Hi @Suthar, Anil ,

    could you please try this:

    $ScriptPath = Get-Location  
    Get-ADComputer -Server <yourDC of abc.com domain> -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt  
    Get-ADComputer -Server <yourDC of anil.abc.com> -Filter {(OperatingSystem -like "windows*server") -and (Enabled -eq "True")} -SearchBase "DC=anil,DC=abc,DC=com" -Properties * | SORT Name | Select -ExpandProperty Name | Out-File $ScriptPath\ServerListNames.txt -Append  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.