Get-ADobject Counts

Quick helpful PS to grab some AD DS info and dump to xlsx. Might come in handy down the road. Requires excel installed on workstation and winrm enabled on domain controller specified in same AD DS domain.

# Prompt end user for domain controller to use for AD DS queries (netbios name will suffice as long as on same domain)
$domaincontroller = Read-Host "Please specify the domain controller to use"

# Start remote PowerShell session with AD DS domain controller specified
Enter-PSSession -ComputerName $domaincontroller

# Create directory off of C: to store resulting xlsx file
New-Item C:\temp\ADDS_Export\ -ItemType Directory

# Declare variables and query AD DS for AD object counts
$aduser = (Get-ADUser -Filter * -ResultSetSize $null | Measure-Object).Count
$adcomputer = (Get-ADComputer -Filter {OperatingSystem -notlike "*Server*"} -ResultSetSize $null | Measure-Object).Count
$adserver = (Get-ADComputer -Filter {OperatingSystem -like "*Server*"} -ResultSetSize $null | Measure-Object).Count
$adgroup = (Get-ADGroup -Filter * -ResultSetSize $null | Measure-Object).Count

# Exit Remote PowerShell session
Exit-PSSession

#Create new Excel object and populate data (requires Excel to be installed on workstation)
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$objExcel.SheetsInNewWorkbook = 1
$Workbook01 = $objExcel.Workbooks.Add()
$Worksheet01 = $Workbook01.Sheets.Item(1)
$Worksheet01.Activate()
$row = 1
$column = 1
$Worksheet01.Cells.Item($row,$column)="User Count"
$column2 = 2
$Worksheet01.Cells.Item($row,$column2)="Computer Count"
$column3 = 3
$Worksheet01.Cells.Item($row,$column3)="Group Count"
$column4 = 4
$Worksheet01.Cells.Item($row,$column4)="Server Count"
$Worksheet01.Cells.Item(2,1)=$aduser
$Worksheet01.Cells.Item(2,2)=$adcomputer
$Worksheet01.Cells.Item(2,3)=$adgroup
$Worksheet01.Cells.Item(2,4)=$adserver
$Workbook01.SaveAs("C:\temp\ADDS_Export\get-adobject.xlsx")
$objExcel.Quit()

Any thoughts?