Skip to content

New-ADReport

Reporting: Generates an HTML report of Active Directory objects

#Requires -Version 5.1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [ValidateSet('User', 'Group', 'Computer')]
    [string]$ObjectType,

    [Parameter(Mandatory = $true)]
    [string]$SearchBase,

    [string[]]$Properties
)

Process {
    try {
        Import-Module ActiveDirectory -ErrorAction Stop

        $cmdArgs = @{ ErrorAction = 'Stop'; SearchBase = $SearchBase }
        if ($PSBoundParameters.ContainsKey('Properties')) { $cmdArgs.Add('Properties', $Properties) }

        $result = switch ($ObjectType) {
            'User' { Get-ADUser -Filter * @cmdArgs | Select-Object Name, SamAccountName, UserPrincipalName, Enabled, Department, Title }
            'Group' { Get-ADGroup -Filter * @cmdArgs | Select-Object Name, SamAccountName, GroupCategory, GroupScope, Description }
            'Computer' { Get-ADComputer -Filter * @cmdArgs | Select-Object Name, SamAccountName, OperatingSystem, Enabled, LastLogonDate }
        }

        if ($null -ne $result) {
            $html = $result | ConvertTo-Html -Head "<style>body{font-family:Arial} table{border-collapse:collapse} th,td{border:1px solid #ddd;padding:8px} th{background-color:#0078D4;color:white}</style>"
            Write-Output $html
        }
        else { Write-Output "No objects found" }
    }
    catch { throw }
}

Type of AD object: User, Group, Computer

Distinguished name of the search base OU

List of properties to include

An interactive directory of PowerShell scripts.