Home > other >  PowerShell Script to check if a computer is a domain controller or not
PowerShell Script to check if a computer is a domain controller or not

Time:10-29

This self-answered question addresses the following scenario:

How can I write a PowerShell script to check if a computer is a domain controller or not?

CodePudding user response:

Yes, if you need to check a list then that's fine, but if all you need is a list of DCs, then more efficient to get this directly.

$DomainName = 'MyDomain'

$DomDetail = Get-ADDomain -Identity $DomainName
$DCDetail = Get-ADDomainController -Server $DomDetail.PDCEmulator -Filter *
[pscustomobject]@{Name = $DomDetail.NetBIOSName;FQDN = $DomDetail.DNSRoot;PDC = $DomDetail.PDCEmulator;MemberDCs = $DCDetail}

CodePudding user response:

The code below can be run on Windows PowerShell. It will take an input list of computers called computers.csv and loop around them to check if it is a domain controller or not and then output the result into check_for_domain_controller.csv

$listofcomputers = Import-CSV -Path "C:\computers_list.csv"

foreach ($computerobject in $listofcomputers)
{
    $computername = $computerobject.Name
    Get-DomainRole -Computername $computername | 
    Export-csv -Path "C:\check_for_domain_controller.csv" -Append -NoTypeInformation
} 

Input (computers.csv)

Name  
DC1  
DC2  
DC3  
DC4  
PC1  
PC2  

Output (check_for_domain_controller.csv)

"Computer","IPAddress","PCType","DomainRole"  
"DC1","10.10.10.1","Desktop","Domain controller"  
"DC2","110.10.10.2","Desktop","Domain controller"  
"DC3","10.10.10.3","Desktop","Domain controller"  
"DC4","10.10.10.4","Desktop","Domain controller"  
"PC1","10.10.10.5","Desktop","Member server"  
"PC2","10.10.10.6","Desktop","Member server"  
  • Related