The Lazy Way To Do Active Directory Inventory
From time to time admins have to run an inventory of what is running in the AD environment. This is a good practice for audits, inventory, removing decommissioned servers, or any other good reason. The details that are required are like when was computer/ server created, when was it last logged into, what is the OS, Service Pack, and OU details if any organization was done in structuring the OU.
Luckily PowerShell can provide all of that information in a nice .csv file which can be later edited in Excel to do filtering as needed.
Open up PowerShell in Admin mode on the DC or create a session if doing this remotely.
<# .SYNOPSIS Get the Inventory of computer objects in the entire forest .DESCRIPTION Get the Inventory of computer objects in the entire forest .INPUTS None. The script captures Forest and Domain details when running in the environment. .OUTPUTS The generated output CSV file will be created in the same path from where the script was executed. .NOTES Version: 1.0 Author: Mohammed Wasay Email: [email protected] Web: www.mowasay.com Creation Date: 04/28/2020 .EXAMPLE Get-Inventory.ps1 #> #Get Logged on user details $cuser = $env:USERDOMAIN + "\" + $env:USERNAME Write-Host -ForegroundColor Gray "Running script as: $cuser authenticated on $env:LOGONSERVER" #Get the Forest Domain $forest = (Get-ADForest).RootDomain #Get all the Domains in the Forest $domains = (Get-ADForest).Domains #Time format for report naming $timer = (Get-Date -Format MM-dd-yyyy) Write-Host -ForegroundColor Magenta "Your Forest is: $forest" #Loop through each domain foreach ($domain in $domains) { Write-Host -ForegroundColor Yellow "Working on Domain: $domain" #Get one domain controller in the domain Import-Module ActiveDirectory $dcs = Get-ADDomainController -Discover -DomainName $domain #Run the following once on the DC foreach ($dc in $dcs.Hostname) { Write-Host -ForegroundColor Cyan "Working on Domain Controller: $dc" #Getting results $results = Get-ADComputer -Filter * -Server $dc -Properties Enabled, Name, DNSHostName, IPv4Address, LastLogonDate, OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion, CanonicalName, whenCreated | Select-Object @{Name = "Domain"; Expression = { $domain } }, Enabled, Name, DNSHostName, IPv4Address, LastLogonDate, OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion, CanonicalName, whenCreated #One results file inclusive of all domains $results | Export-Csv ./$forest-Servers-$timer.csv -NoTypeInformation -Append #Seperate results file for each domain #$results | Export-Csv ./$domain-Servers-$timer.csv -NoTypeInformation } Write-Host -ForegroundColor Green "Report for $domain generated!" } Write-Host -ForegroundColor Green "------======= Done! =======------"
Result:

Author
Related Posts
Get all the domains controllers in the AD forest along with their current FSMO roles
In a large enterprise an admin would need to keep track of all the domains in a AD forest, the domain names,...
Read out all
Force synchronization for DFSR-replicated SYSVOL
One of my clients had a problem with processing GPO on client computers. Different computers applied different settings from the same GPO...
Get Inactive Users Report for the past 60 days in a multi domain environment
I had a request recently to provide an inactive user report for the past 60 days. Basically, find out which accounts have...
Get Primary, Secondary, Tertiary DNS values and more from Multiple Servers
Came across a unique request to get primary, secondary, and tertiary DNS values for multiple computers/servers across the domain. I started writing...
Fix Active Directory broken security inheritance problem
Ran into a situation at a client location where in Active Directory, the security permissions applied to an OU were not getting...
How to Fix: Attribute userAccountControl of DC is: 0x82020
When running a DCDiag at a customer site today I had the following error occur: [su_box title=”” box_color=”#f3f1cb” title_color=”#000000″ radius=”6″]Warning: Attribute userAccountControl...