PowerShell Script - List Certain ADGroups as Column Headings & their Users as Row Headings showing who is in which groups
How do I do this in PowerShell please?
I can only find a way to list Users in groups or list groups for a user, not list both in this way.
List AdUsers (in certain groups) as row headings and a subset of AdGroups as Column headings (eg: Group1, Group3 & Group10 only) with the column values having 1’s or Y’s to show which of those groups the users are in.
eg:
Group1 members are:
UserA
UserC
Group3 members are:
UserA
UserB
Group10 members are:
UserB
UserC
What I'm expecting:
User Group1 Group3 Group10
UserA 1 1
UserB 1 1
UserC 1 1
I have this so far, but want to add the matrix shown above as a summary worksheet in the Excel file.
# The ActiveDirectory (AD) App generally only lists Staff Ids which is painful when trying to find out each staff members name to determine who needs to be removed from what groups.
# This script will:
# * Export certain properties of AD Group Members (eg: StaffIds, Names, Depts, etc) for each SSRS AD Group that <Dept> uses for <Dept> RAP Report Security.
# * Exports the data to an Excel Spreadsheet with 1 worksheet per AD Group, listing it's members.
#
# PRE-REQUISITES:
# * Export-Excel module must be installed
# * ActiveDirectory module must be installed
# ** Remote Server Administration Tools (RSAT) must be installed
# --------------------------------------------------------------------------
# Shows what Properties are available that can be added to lines 30-35 below
#Get-ADUser 12345678 -Properties *
Clear-Host
# Set the filename for the output Excel file
$dateTimeStr = (get-date).tostring('yyyyMMdd_HHmmss')
[String]$ExcelPath = "C:\Temp\DeptSsrsAdGroups_$dateTimeStr.xlsx"
# Set the list of AD Groups to export results for
[String[]]$AdGroups = ("Group1", "Group3", "Group10")
# Loop through each AD Group in the list
# Export certain properties/fields for each user/member in the AD Group to a worksheet with the same name as the AD Group
# Each excel worksheet will have a table with the AD Group name, auto sized and the top row frozen for formatting/layout purposes.
ForEach($AdGroup in $AdGroups)
{
Get-AdGroupMember -Identity $AdGroup -Recursive | `
Get-ADUser -Properties SamAccountName,Surname,GivenName,DisplayName,EmailAddress,Enabled,Title,Department,Office,Company,Manager | `
#Select SamAccountName,Surname,GivenName,DisplayName,EmailAddress,Enabled,Title,Department,Office,Company,Manager | `
Select SamAccountName,Surname,GivenName,DisplayName,EmailAddress,Enabled,@{n="ManagerName";e={(Get-AdUser $_.manager -Properties DisplayName).DisplayName}},Title,Department,Office,Company | `
Sort-Object Surname,GivenName | `
Export-Excel -Path $ExcelPath -AutoSize -WorksheetName $AdGroup -TableName $AdGroup -FreezeTopRow
}
# --------------------------------------------------------------------------