How to Get List of Active Computers Using Powerhsell?

James Thompson 0 Reputation points
2023-05-18T08:48:08.5666667+00:00

Looking for help on a Powershell to do a recursive search and return all active computers but only in certain OU's (including their sub OU's) in each domain

Environment = 3 domains in a single forest

OU path for each domain = domain\bu\bu_name\computers

There are 2 OU's under the computers in that path (one for desktops / one for laptops)

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Tushar Kumar 3,371 Reputation points MVP
    2023-05-18T09:23:34.8233333+00:00

    #

     Define the target OU paths
    $ouPaths = @(
        "domain1\bu\bu_name\computers",
        "domain2\bu\bu_name\computers",
        "domain3\bu\bu_name\computers"
    )
    # Iterate over each domain
    foreach ($ouPath in $ouPaths) {
        # Extract the domain name from the OU path
        $domain = $ouPath.Split("\")[0]
        # Connect to the domain
        $domainContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain", $domain)
        $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($domainContext)
        $domainDN = $domain.GetDirectoryEntry().distinguishedName
        # Perform a recursive search within the target OU
        $searcher = New-Object System.DirectoryServices.DirectorySearcher
        $searcher.SearchRoot = "LDAP://$domainDN"
        $searcher.Filter = "(&(objectClass=computer)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"
        $searcher.SearchScope = [System.DirectoryServices.SearchScope]::Subtree
        # Retrieve and display the active computer names
        $results = $searcher.FindAll()
        foreach ($result in $results) {
            $computer = $result.Properties["name"]
            Write-Host "Active computer found in $ouPath: $computer"
        }
        # Clean up the searcher object
        $searcher.Dispose()
    }
    

    Hope this helps!!

    0 comments No comments

  2. James Thompson 0 Reputation points
    2023-05-18T09:49:51.67+00:00

    Here is the code I started with:

    #Load PS AD Module
    Import-Module -Name ActiveDirectory
    
    #Set Variables
    $domains = (Get-ADForest).Domains
    $OUs = "OU=gbu,OU=BUs,DC=am,DC=contoso,DC=net","OU=gbu,OU=BUs,DC=apac,DC=contoso,DC=net","OU=gbu,OU=BUs,DC=emea,DC=contoso,DC=net"
    $data = @()
    
    #Get list of active computers in each domain and OU
    foreach ($domains in $domains)
    {
        $data += Get-ADComputer -Filter "Enabled -eq 'True'" -SearchBase $OUs -SearchScope 2 | Select-Object Name, DistinguishedName
    }
    
    #Export to CSV file
    

  3. Rich Matheisen 47,901 Reputation points
    2023-05-18T19:06:28.81+00:00

    Try this (change the PATH in the Export-CSV first!):

    $OUs = "OU=gbu,OU=BUs"
    
    (Get-ADForest).Domains |
        ForEach-Object{
            $DomainDn = (Get-ADDomain $_).distinguishedName
            $DC = Get-ADDomainController -Discover -Domain $_
            ForEach ($OU in $OUs){
                $OUDN = "{0},{1}" -f $OU, $DomainDn
                Get-ADComputer -Filter "Enabled -eq 'True'" -Server $DC -SearchBase $OUDN -SearchScope Subtree | Select-Object Name, DistinguishedName
            }
        } | Export-Csv c:\junk\AdComputers -notypeinfo
    
    0 comments No comments

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.