Forum Discussion

robmo's avatar
robmo
Brass Contributor
Feb 17, 2023
Solved

Checking if a computer is in the the Active Directory global catalog

Hi, I need a script that can tell me if a computer is found in the Active Directory global catalog. The code below is returning inaccurate results. Every computer that failed the Try statement can b...
  • robmo's avatar
    robmo
    Feb 17, 2023

    I added some bogus computer names to computers.txt along with some known computers and it seems to be working now. Let me know if this solution doesn't seem correct.

     

    Import-Module ActiveDirectory
    $server = "myserver.com:3268"
    
    #Get the list of computers to test
    $computers = Get-Content -Path "$PSScriptRoot\computers.txt"
    
    #Check active directory for each computer
    foreach($computer in $computers) {
    
        $test = Get-ADComputer -Filter 'Name -eq $computer' -Server $server
    
        if($null -ne $test) {
            Write-Host "$computer in AD" -ForegroundColor Green
        } else {
            Write-Host "$computer not in AD" -ForegroundColor Red
        }
    
    }

     

Resources