AD Script: Sharing my PowerShell script to create a report of all AD-users information & membership
This time I am sharing my script for exporting user information & their AD-group membership via powershell. Feel free to use it as you'd like or suggest improvements :)
Output will look like this in the .csv-file it generates:
Type, Full Name, Username, Company, Department, Title, Manager, Managers initials, Last logon date, Last password change, Groupname, Group description.
Get-ADGroup -filter * -Properties * | Select SamAccountName, Description |
Export-csv C:\Users\$ENV:Username\Desktop\ADGroups.csv -NoTypeInformation -Encoding UTF8
cls
Write-Host "Starting Export.. The script is estimated to take about 30 minutes to complete." -ForegroundColor Green
$ErrorActionPreference= 'silentlycontinue'
$csv = Import-csv C:\Users\$ENV:Username\Desktop\ADGroups.csv
foreach ($row in $csv) {
Get-ADGroupMember -Identity $row.SamAccountName |
Get-ADObject -Properties * |
Select-Object @{Name="Type";Expression={$_.ObjectClass}},
@{Name="Full Name";Expression={$_.DisplayName}},
@{Name="Username";Expression={$_.SamAccountName}},
@{Name="Company";Expression={$_.Company}},
@{Name="Department";Expression={$_.Department}},
@{Name="Title";Expression={$_.Title}},
@{Name="Manager";Expression={(Get-ADUser -property DisplayName $_.Manager).DisplayName}},
@{Name="Managers initials";Expression={(Get-ADUser -property SamAccountName $_.Manager).SamAccountname}},
@{Name="Last logon date";Expression={(Get-ADUser -property LastLogonDate $_.SamAccountName).LastLogonDate}},
@{Name="Last password change";Expression={(Get-ADUser -property PasswordLastSet $_.SamAccountName).PasswordLastSet}},
@{Name="Groupname";Expression={$row.SamAccountName}},
@{Name="Group description";Expression={$row.Description}} |
Export-csv "C:\Users\$ENV:Username\Desktop\Export.csv" -NoTypeInformation -Encoding UTF8 -Append}
If you want to remove all AD computer-objects from the export use the following lines in the Export-csv cmdlet. The output file will then be named "Permissions.csv" instead of "Export.csv"
Export-csv "C:\Users\$ENV:Username\Desktop\Export.csv" -NoTypeInformation -Encoding UTF8 -Append}
Import-Csv C:\Users\$ENV:Username\Desktop\Export.csv| where {$_.Type -ne "Computer"} |
Export-Csv C:\Users\$Env:username\Desktop\Permissions.csv -NoTypeInformation -Encoding UTF8
Remove-Item "C:\Users\$ENV:Username\Desktop\export.csv" -Force
Posted Dec 19, 2019, 12:05 PM UTC
Thanks for sharing!
Not to belittle your script, but I one of the things I like to do that also helps me learn is to take others' scripts and modify them using different methods.
It helps me think of alternative ways to accomplish similar tasks, and it shares the knowledge that I have with others at the same time.
And in that note, here is what I came up with:
It is beautiful mate, thanks for sharing. Normally I just do * because I'm lazy, and in a way think it's unnecessary to write the same object names 2 times but yeah, it does increase the speed of the script :)
I totally agree on using
*because I'm lazy, but whenever I'm working with large sets of data (like in this case), I have forced myself to get in to the habit of not using*as it can drastically reduce the speed of my scriptsI'm just now getting into PowerShell scripting, never done scripting outside of batch files before, and this is a very good tip. Thanks!
I also recommend looking at this helpful guide I wrote to using the
-filterparameter with the AD CmdLetsI love this subreddit ♡