Active Directory PowerShell scripts for generating user reports

Managing Active Directory (AD) is a core responsibility for IT admins, and a crucial aspect of that role involves tracking user activity; auditing changes; and producing accurate, real-time reports for compliance purposes. For many IT teams, PowerShell continues to be the preferred tool for both AD management and reporting. From tracking user logons and overseeing group memberships to analyzing Microsoft 365 user activity, PowerShell offers the flexibility and precision needed to maintain a secure, compliant, and well-managed AD environment.

While PowerShell offers powerful capabilities for these tasks, it also presents several challenges. Crafting and maintaining complex scripts can be incredibly time-consuming, demanding advanced scripting knowledge. Furthermore, PowerShell lacks centralized reporting features, which increases the risk of human error and potential security gaps. ADManager Plus, an AD reporting solution, offers predefined AD user reports that provide complete visibility on AD users in just a few clicks, eliminating the need for complex scripting.

Essential PowerShell commands for AD user reporting

Understanding the core PowerShell cmdlets is the first step towards mastering AD reporting. Here are some fundamental commands you'll frequently use:

  • Get-ADUser: This command helps retrieve user information from AD. You can use various attributes like SamAccountName, Enabled, LastLogonDate, and more as parameters to filter out the results.
  • Get-ADGroupMember: This command lets you identify which groups a user belongs to.
  • Search-ADAccount: This command is excellent for finding accounts based on their status, such as locked-out accounts, inactive accounts, or accounts with expired passwords.

PowerShell scripts for common Active Directory user reports

Here is a collection of essential AD PowerShell scripts that can help you manage and report on your user environment effectively.

1. List all AD users

This foundational script is vital for baseline auditing, directory cleanups, or migrating user data. It retrieves a comprehensive list of all AD users along with their key properties, and also exports AD users to a CSV for easy analysis.

Get-ADUser -Filter * -Properties * |
Select-Object Name, sAMAccountName, userPrincipalName, Enabled, department, title, LastLogonDate |
Export-Csv -Path "C:\AllADUsers.csv" -NoTypeInformation

2. Get active AD users

While finding disabled users is key for cleanup, getting a list of all active and enabled accounts is essential for daily management, licensing audits, and security base lining. This script filters for all user accounts that are currently enabled and ready for use.

Get-ADUser -Filter 'Enabled -eq $true' -Properties DisplayName, sAMAccountName, LastLogonDate | Select-Object name, displayName, sAMAccountName, LastLogonDate | Export-Csv -Path "C:\Reports\EnabledUsers.csv" -NoTypeInformation

3. Find inactive AD users

Identifying inactive accounts is a critical security measure to prevent unauthorized access and maintain a clean AD environment. This script helps pinpoint users who haven't logged in for a specified period.

$InactiveDays = 90$InactiveDate = (Get-Date).AddDays(-$InactiveDays)
Get-ADUser -Filter {LastLogonDate -lt $InactiveDate Enabled -eq $true} -Properties LastLogonDate | Select-Object Name, sAMAccountName, LastLogonDate

4. Get disabled AD accounts

Disabled accounts, if not properly managed, can pose a security risk or indicate former employees who still have a lingering directory presence. This script helps you quickly fetch all disabled AD users, providing their name, sAMAccountName, and their enabled status for clear identification.

Get-ADUser -Filter {Enabled -eq $false} -Properties Name, sAMAccountName, Enabled | Select-Object Name, sAMAccountName, Enabled

5. Find account expired AD users

Use this script to find temporary or contract user accounts that have passed their set expiration date. This is different from a disabled account and is useful for cleaning up accounts that should no longer have access.

Search-ADAccount -AccountExpired | Select-Object Name, sAMAccountName, AccountExpirationDate | Export-Csv -Path "C:\Reports\ExpiredAccounts.csv" -NoTypeInformation

6. Get AD users' last logon date

Tracking the last logon date for AD users is an essential practice for security auditing, identifying inactive accounts, and maintaining a clean user directory. This AD user logon PowerShell script retrieves the last logon timestamp for all users, providing their name, sAMAccountName, and the last logon date.

Get-ADUser -Filter * -Properties LastLogonDate |
Select-Object Name, sAMAccountName, LastLogonDate |
Export-Csv -Path "C:\ADUserLogonReport.csv" -NoTypeInformation

7. List AD user permissions

Generating reports on AD users' permissions is essential for auditing user access and ensuring a least-privilege security posture. This AD user permissions PowerShell script retrieves the permissions that a user has on a specific organizational unit (OU).

$OUPath = "OU=Sales,DC=yourdomain,DC=com"
Get-Acl -Path"AD:\$OUPath" | Select-Object-ExpandProperty Access | Select-Object IdentityReference, ActiveDirectoryRights, AccessControlType, ObjectType, InheritanceType, IsInherited | Export-Csv-Path"C:\Reports\OUPermissions.csv"-

8. Find user accounts with passwords set to never expire

For security reasons, very few accounts should have their passwords set to never expire. This script identifies all user accounts that have this flag enabled:

Get-ADUser -Filter * -Properties name, PasswordNeverExpires | Where-Object {$_.PasswordNeverExpires -eq $true} | Select-Object name, sAMAccountName, distinguishedName | Export-Csv -Path "C:\Reports\PwdNeverExpires.csv" -NoTypeInformation

Sometimes you just need to quickly find a single user's details without navigating the Active Directory Users and Computers interface. This script allows you to find a specific user by their sAMAccountName and retrieve all of their properties:

$Username = "JohnDoe"
Get-ADUser -Identity $Username -Properties *

9. Get AD users with their managers

For organizational charting, reporting structures, or approval workflows, you often need a list of users and their direct managers. This script retrieves users and includes their manager's name in the report.

Get-ADUser -Filter * -Properties displayName, manager | Select-Object name, displayName, @{Name="Manager";Expression={(Get-ADUser $_.Manager).Name}} | Export-Csv -Path "C:\Reports\UsersWithManagers.csv" -NoTypeInformation

10. Get a user's sAMAccountName

If you have a user's full name but need their sAMAccountName, this script provides a quick way to look it up.

$DisplayName = "John Doe"
Get-ADUser - Filter "displayName -eq '$displayName'" | Select-Object name, sAMAccountName

11. Get users from multiple OUs

In complex AD environments, users may be spread across several OUs. This script demonstrates how to target multiple OUs and consolidate the user list into a single report.

Get-ADUser-Filter * -SearchBase$OU-Properties displayName | Select-Object name, displayName, distinguishedName } $AllUsers | Export-Csv-Path"C:\Reports\MultiOUUsers.csv"-NoTypeInformation

12. Users from a specific department

Filtering users by department is a common requirement for creating department-specific distribution lists or reports. This script retrieves all users who have a specific value in their department attribute.

$Department = "Finance"
Get-ADUser -Filter "department -eq '$department'" -Properties displayName, department | Select-Object name, displayName, sAMAccountName, department | Export-Csv -Path "C:\Reports\FinanceDepartmentUsers.csv" -NoTypeInformation

13. Users with empty attributes

For data integrity and AD cleanup, it's useful to find users who are missing important information, such as a phone number or email address. This script finds all users where the TelephoneNumber attribute is not set. You can change the attribute to whatever you need to check.

Get-ADUser-Filter * -Properties telephoneNumber | Where-Object {-not $_.TelephoneNumber} | Select-Object name, sAMAccountName, distinguishedName | Export-Csv-Path"C:\Reports\UsersWithEmptyPhone.csv"-NoTypeInformation

14. Users with a specific Common-Name

While name and displayName are the most commonly used naming attributes, sometimes you need to find a user based on their Common-Name (CN), which is part of their distinguished name. This script shows how to filter users based on the CN.

$CN = "John Doe"
Get-ADUser -Filter "cn -eq '$cn'" | Select-Object Name, sAMAccountName, distinguishedName

Challenges of using PowerShell scripts for AD user reporting

While powerful, relying solely on PowerShell AD scripts for extensive reporting can present several challenges:

  • Time-consuming: Writing, testing, and maintaining complex scripts for diverse reports can be a significant drain on resources.
  • Complexity: Advanced reporting often requires intricate scripting knowledge, especially for filtering, joining data, and handling large datasets.
  • Lack of centralization: Script outputs can be scattered, making it difficult to get a unified view of your AD environment.
  • Manual reporting: Generating ad-hoc reports quickly can be challenging without prebuilt scripts.
  • Error prone: Manual scripting increases the risk of human error, potentially leading to inaccurate data.
  • Security concerns: Granting permissions for users who need to run scripts can lead to security gaps.

Simplify AD user reporting with ADManager Plus

ADManager Plus is a comprehensive AD reporting tool designed to simplify AD, Exchange, Microsoft 365, and Google Workspace management and reporting. It comes with the following capabilities:

  • Over 200 predefined reports: Access a vast library of over 200 ready-to-use reports on users, groups, computers, GPOs, and more, no scrips required.
  • Automated reporting: Schedule AD reports to be generated and delivered to your inbox automatically, ensuring you always have up-to-date information.
  • Customized reporting: Use filters to create highly specific reports tailored to your exact requirements.
  • Exportable reports: Export AD reports to various formats like CSV, PDF, HTML, and XLSX to meet audit requirements.
  • Delegated reporting: Delegate reporting tasks to help desk technicians without granting them extensive permissions, enhancing security and reducing workload.
  • Instant administration: Identify issues from reports and take immediate corrective actions directly within the same console, streamlining your workflow.

No more scripting, generate AD user reports instantly!

FAQs

You can use the Get-ADUser cmdlet with the -Filter * parameter to get all users.

You can use the Export-Csv cmdlet to export any PowerShell object to a CSV file.

You can use the Get-ADUser cmdlet with the -Properties LastLogonDate parameter to retrieve a user's last logon time.

You can use the Search-ADAccount -LockedOut cmdlet to find all locked-out user accounts.

Yes, you can use the Get-Acl cmdlet to get the permissions for any AD object, including OUs, users, and groups.

 
  • Essential PowerShell commands for AD user reporting
  • PowerShell scripts for common Active Directory user reports
  • Challenges of using PowerShell scripts for AD user reporting
  • Simplify AD user reporting with ADManager Plus
The one-stop solution to Active Directory Management and Reporting
Email Download Link