Skip to content

Get Active Directory information with PowerShell script

How to get Active Directory info in one output? For example, you want to migrate Active Directory to a new server, and you like to get the AD info. Or you like to know how many workstations, servers, or groups are present in AD? In this article, you will learn how to get Active Directory information with PowerShell script.

Get AD info PowerShell script

The Get-ADInfo.ps1 PowerShell script will get the following AD information:

  1. Computers (Workstations + Servers)
  2. Workstations
  3. Servers
  4. Users
  5. Groups
  6. Active Directory forest name
  7. Active Directory forest mode
  8. Active Directory domain mode
  9. Active Directory schema version
  10. FSMO role owners

Download get AD info PowerShell script

Download and place Get-ADInfo.ps1 PowerShell script in the C:\scripts folder. If you don’t have a scripts folder, create one.

Ensure that the file is unblocked to prevent any errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.

Another option is to copy and paste the below code into Notepad. Give it the name Get-ADInfo.ps1 and place it in the C:\scripts folder.

<#
    .SYNOPSIS
    Get-ADInfo.ps1

    .DESCRIPTION
    Get Active Directory information.

    .LINK
    alitajran.com/active-directory-information-powershell-script

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.alitajran.com
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 01/07/2023 - Initial version
    V1.10, 11/05/2023 - Made it all PowerShell commands
#>

# Get counts of different types of objects in Active Directory
$Computers = (Get-ADComputer -Filter * | Measure-Object).Count
$Workstations = (Get-ADComputer -Filter { OperatingSystem -notlike "*Server*" } | Measure-Object).Count
$Servers = (Get-ADComputer -Filter { OperatingSystem -like "*Server*" } | Measure-Object).Count
$Users = (Get-ADUser -Filter * | Measure-Object).Count
$Groups = (Get-ADGroup -Filter * | Measure-Object).Count

# Get Active Directory Forest information
$ADForest = (Get-ADDomain).Forest
$ADForestMode = (Get-ADForest).ForestMode
$ADDomainMode = (Get-ADDomain).DomainMode

# Obtain Active Directory Schema version and translate it to the corresponding Windows Server version
$ADVer = Get-ADObject (Get-ADRootDSE).schemaNamingContext -property objectVersion | Select-Object objectVersion
$ADNum = $ADVer -replace "@{objectVersion=", "" -replace "}", ""
If ($ADNum -eq '88') { $srv = 'Windows Server 2019/Windows Server 2022' }
ElseIf ($ADNum -eq '87') { $srv = 'Windows Server 2016' }
ElseIf ($ADNum -eq '69') { $srv = 'Windows Server 2012 R2' }
ElseIf ($ADNum -eq '56') { $srv = 'Windows Server 2012' }
ElseIf ($ADNum -eq '47') { $srv = 'Windows Server 2008 R2' }
ElseIf ($ADNum -eq '44') { $srv = 'Windows Server 2008' }
ElseIf ($ADNum -eq '31') { $srv = 'Windows Server 2003 R2' }
ElseIf ($ADNum -eq '30') { $srv = 'Windows Server 2003' }

# Display collected information
Write-host "Active Directory Info" -ForegroundColor Yellow
Write-host ""
Write-Host "Computers  = $Computers" -ForegroundColor Cyan
Write-Host "Workstions = $Workstations" -ForegroundColor Cyan
Write-Host "Servers    = $Servers" -ForegroundColor Cyan
Write-Host "Users      = $Users" -ForegroundColor Cyan
Write-Host "Groups     = $Groups" -ForegroundColor Cyan
Write-host ""
Write-Host "Active Directory Forest Name = "$ADForest -ForegroundColor Cyan
Write-Host "Active Directory Forest Mode = "$ADForestMode -ForegroundColor Cyan
Write-Host "Active Directory Domain Mode = "$ADDomainMode -ForegroundColor Cyan
Write-Host "Active Directory Schema Version is $ADNum which corresponds to $srv" -ForegroundColor Cyan
Write-Host ""
Write-Host "FSMO Role Owners" -ForegroundColor Cyan

# Retrieve FSMO roles individually
$Forest = Get-ADForest
$SchemaMaster = $Forest.SchemaMaster
$DomainNamingMaster = $Forest.DomainNamingMaster
$Domain = Get-ADDomain
$RIDMaster = $Domain.RIDMaster
$PDCEmulator = $Domain.PDCEmulator
$InfrastructureMaster = $Domain.InfrastructureMaster

# Display FSMO role owners
Write-Host "Schema Master         =  $SchemaMaster" -ForegroundColor Cyan
Write-Host "Domain Naming Master  =  $DomainNamingMaster" -ForegroundColor Cyan
Write-Host "RID Master            =  $RIDMaster" -ForegroundColor Cyan
Write-Host "PDC Emulator          =  $PDCEmulator" -ForegroundColor Cyan
Write-Host "Infrastructure Master =  $InfrastructureMaster" -ForegroundColor Cyan

Run get AD info PowerShell script

Run PowerShell as administrator. Next, run the PowerShell script to gather the Active Directory information.

C:\scripts\.\Get-ADInfo.ps1

This is how the output looks like in our organization.

Active Directory Info

Computers  = 5
Workstions = 1
Servers    = 4
Users      = 74
Groups     = 88

Active Directory Forest Name =  exoip.local
Active Directory Forest Mode =  Windows2016Forest
Active Directory Domain Mode =  Windows2016Domain
Active Directory Schema Version is 88 which corresponds to Windows Server 2019/Windows Server 2022

FSMO Role Owners
Schema Master         =  DC01-2019.exoip.local
Domain Naming Master  =  DC01-2019.exoip.local
RID Master            =  DC01-2019.exoip.local
PDC Emulator          =  DC01-2019.exoip.local
Infrastructure Master =  DC01-2019.exoip.local

Here is a screenshot of what it looks like.

Get Active Directory information with PowerShell script

That’s it!

Read more: Get all Domain Controllers with PowerShell »

Conclusion

You learned how to get Active Directory information with PowerShell script. There is a lot of information in Active Directory, and searching for the info one by one in PowerShell or the GUI takes a lot of time. Running a PS script and having it all in one output saves time and is easier to look at.

Did you enjoy this article? You may also like Export AD users to CSV with PowerShell. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *