PowerShell Script: Validate if a computer account exists in Active Directory

The script below will let you quickly validate whether given computers exist in Active Directory. Please keep both script and Computer.txt in the same location. Add the computer NetBIOS name in the text file, separated by a new line. The result will be saved in the Output.csv file in the script folder.

$File = "$PSScriptRoot\Computers.txt" 
$LogFile = "$PSScriptRoot\Output.csv" 
 
# Function to WriteLog File 
Function WriteLog($Msg) 
{ 
    $Text = $Msg 
    Add-Content -Path $LogFile  $text  
    Write-Host $Text 
} 
  
$ComputerList = Get-Content -Path $File 
$TotalRecord = ($ComputerList).Count 
foreach ($computer in $ComputerList){ 
    $error.clear() 
    try{$ADComputer = Get-ADComputer $computer -Properties *} 
    catch{ WriteLog "$computer,Does not Exists in AD"} 
    if (!$error) {  WriteLog "$computer,Computer exists in AD"} 

}

Related Posts:

Subscribe to Techuisitive Newsletter

Be the first to know about our new blog posts. Get our newsletters directly in your inbox and stay up to date about Modern Desktop Management technologies & news.

Scroll to Top