Report on user count in Active Directory with PowerShell
Here is quick PowerShell script that provides information on user count in Active directory:
- Total Number of All user accounts
- Total Number of Enabled accounts
- TotalNumber of Disabled accounts
It outputs in to formatted table in html so you can send in email as attachment or just keep for records in share drive.
# Import the Active Directory module if not already loaded
if (-not (Get-Module ActiveDirectory)){
Import-Module ActiveDirectory -ErrorAction Stop
}
Write-Host
Write-Host ACTIVE DIRECTORY USER COUNT -ForegroundColor white -BackgroundColor red
Write-Host Users in AD including Disabled users: (Get-ADUser -filter *).count
Write-Host
Write-Host ENABLED vs. DISABLED -ForegroundColor White -BackgroundColor Red
Write-Host ENABLED : (Get-AdUser -filter * | Where {$_.enabled -eq "True"}).count
Write-Host DISABLED: (Get-ADUser -filter * | Where {$_.enabled -ne "False"}).count
$TotalAccounts = (Get-ADUser -filter *).count
$EnabledUsers = (Get-AdUser -filter * | Where {$_.enabled -eq "True"}).count
$DisabledUsers = (Get-ADUser -filter * | Where {$_.enabled -ne "False"}).count
#export to HTML Report
$html ="
<header><style>
table {<br /> border-collapse: collapse;<br /> }<br /> td, th {<br /> border: 1px solid black;<br /> }<br /> </style></header>
<table>
<tbody>
<tr>
<td>Total Number of All user accounts in Active Directory</td>
<td>$TotalAccounts</td>
</tr>
<tr>
<td>Total Number of Enabled Users in Active Directory</td>
<td>$EnabledUsers</td>
</tr>
<tr>
<td>TotalNumber of Disabled Users in Active Directory</td>
<td>$DisabledUsers</td>
</tr>
</tbody>
</table>
"
$html | Out-File "TotalUsersADReport.html"
Write-host
Write-host => Report TotalUsersADReport.html has been created ! .... -f yellow
if (-not (Get-Module ActiveDirectory)){
Import-Module ActiveDirectory -ErrorAction Stop
}
Write-Host
Write-Host ACTIVE DIRECTORY USER COUNT -ForegroundColor white -BackgroundColor red
Write-Host Users in AD including Disabled users: (Get-ADUser -filter *).count
Write-Host
Write-Host ENABLED vs. DISABLED -ForegroundColor White -BackgroundColor Red
Write-Host ENABLED : (Get-AdUser -filter * | Where {$_.enabled -eq "True"}).count
Write-Host DISABLED: (Get-ADUser -filter * | Where {$_.enabled -ne "False"}).count
$TotalAccounts = (Get-ADUser -filter *).count
$EnabledUsers = (Get-AdUser -filter * | Where {$_.enabled -eq "True"}).count
$DisabledUsers = (Get-ADUser -filter * | Where {$_.enabled -ne "False"}).count
#export to HTML Report
$html ="
<header><style>
table {<br /> border-collapse: collapse;<br /> }<br /> td, th {<br /> border: 1px solid black;<br /> }<br /> </style></header>
<table>
<tbody>
<tr>
<td>Total Number of All user accounts in Active Directory</td>
<td>$TotalAccounts</td>
</tr>
<tr>
<td>Total Number of Enabled Users in Active Directory</td>
<td>$EnabledUsers</td>
</tr>
<tr>
<td>TotalNumber of Disabled Users in Active Directory</td>
<td>$DisabledUsers</td>
</tr>
</tbody>
</table>
"
$html | Out-File "TotalUsersADReport.html"
Write-host
Write-host => Report TotalUsersADReport.html has been created ! .... -f yellow
Feel free to modify as you wish to your own needs.