Introduction
This PowerShell script provides a simple way to export common attributes of all computer objects within Active Directory to a CSV file. The script is designed to be easily adaptable for different environments and use cases.
Use Case
This script can be useful for:
- Documentation: Maintaining an up-to-date inventory of computer objects in your Active Directory domain.
- Auditing: Periodically exporting computer object data for analysis and compliance checks.
- Reporting: Generating reports on computer object attributes like operating system versions or last logon times.
Script
PowerShell
# Specify the domain controller and domain name
$domainController = "<Your Domain Controller>"
$domainName = "<Your Domain Name>"
# PowerShell Script to Export AD Object Information to CSV with Date in Filename
$todayDate = (Get-Date).ToString("yyyy-MM-dd")
$csvPath = "C:\Exports\AD_$todayDate.csv"
# Retrieve AD Objects from the specified domain controller
$ADObjects = Get-ADComputer -Filter * -Server $domainController -Property CanonicalName, Description, DistinguishedName, DNSHostName, Enabled, IPv4Address, LastLogonDate, Name, ObjectClass, ObjectGUID, OperatingSystem, OperatingSystemVersion, PasswordLastSet, SamAccountName, SID, UserPrincipalName
$ADObjects | Select-Object CanonicalName, Description, DistinguishedName, DNSHostName, Enabled, IPv4Address, LastLogonDate, Name, ObjectClass, ObjectGUID, OperatingSystem, OperatingSystemVersion, PasswordLastSet, SamAccountName, SID, UserPrincipalName |
Export-Csv -Path $csvPath -NoTypeInformation
Write-Host "CSV export complete. File saved to $csvPath"
Explanation
- Variables: The script starts by defining variables for the domain controller, domain name, and the desired output file path. The filename includes the current date for easy tracking.
- Get-ADComputer: The
Get-ADComputer
cmdlet retrieves all computer objects from the specified domain controller. The-Property
parameter specifies the attributes to be retrieved. - Select-Object: This cmdlet selects the desired attributes from the retrieved computer objects.
- Export-Csv: The
Export-Csv
cmdlet exports the selected attributes to a CSV file at the specified path.
Outro
This script is a basic example of how to export Active Directory computer object data to a CSV file. You can modify the script to include additional attributes, filter the computer objects based on specific criteria, and customize the output file path and name.
Tags
PowerShell, Active Directory, Export, CSV, Automation, Scripting