We have two security groups in Active Directory, and we want to compare the members…
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.
Table of contents
Get AD info PowerShell script
The Get-ADInfo.ps1 PowerShell script will get the following AD information:
- Computers (Workstations + Servers)
- Workstations
- Servers
- Users
- Groups
- Active Directory forest name
- Active Directory forest mode
- Active Directory domain mode
- Active Directory schema version
- 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.
<#
Author: ALI TAJRAN
Website: alitajran.com
LinkedIn: linkedin.com/in/alitajran
Date: 01/07/2023
Description: Get Active Directory information
#>
# Get AD info
$Computers = (Get-ADComputer -Filter *).count
$Workstations = (Get-ADComputer -LDAPFilter "(&(objectClass=Computer)(!operatingSystem=*server*))" -Searchbase (Get-ADDomain).distinguishedName).count
$Servers = (Get-ADComputer -LDAPFilter "(&(objectClass=Computer)(operatingSystem=*server*))" -Searchbase (Get-ADDomain).distinguishedName).count
$Users = (Get-ADUser -Filter *).count
$Groups = (Get-ADGroup -Filter *).Count
$ADForest = (Get-ADDomain).Forest
$FSMO = netdom query FSMO
$ADForestMode = (Get-ADForest).ForestMode
$ADDomainMode = (Get-ADDomain).DomainMode
$ADVer = Get-ADObject (Get-ADRootDSE).schemaNamingContext -property objectVersion | Select 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' }
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
$FSMO
Run get AD info PowerShell script
Run PowerShell as administrator. Change the path to the scripts folder. Then, run the PowerShell script to gather the Active Directory information.
PS C:\> cd c:\scripts
PS C:\scripts> .\Get-ADInfo.ps1
This is how the output looks like in our organization.
Active Directory Info
Computers = 8
Workstions = 3
Servers = 5
Users = 5143
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
PDC DC01-2019.exoip.local
RID pool manager DC01-2019.exoip.local
Infrastructure master DC01-2019.exoip.local
The command completed successfully.
Here is a screenshot of what it looks like.
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.
This Post Has 0 Comments