Forum Discussion
Script to get AD group and members details but group name is to long
Hi, Clinton.
That script might work but there's a lot wrong with it.
The 30 character limit relates explicitly to the Excel worksheet name, which cannot be longer than 30 characters. So, that's the only place you'd possibly be truncating the group name.
The issue with adjusting the group name though is that it may no longer then be unique (particularly likely if your organisation has strong naming conventions), which can then cascade into the undesirable outcome of merging what should be multiple worksheets into one. So, you have some thinking to do there as to how you want to handle that 30 character limitation for the worksheet name.
Anyhow, I'm not looking to specifically solve the 30 character limit or the worksheet merging issues for you. I'm only taking your original example script and cleaning up the structural issues.
Get-ADGroup -Properties Name -Filter * | ForEach-Object {
$group = $_.Name;
if ($group.Length -gt 30)
{
$WorksheetName = $group.Substring(0, 30);
}
else
{
$WorksheetName = $group;
}
Get-ADGroupMember -Identity $group -Recursive |
Where-Object {$_.objectClass -eq 'user'} |
Get-ADUser -Properties Displayname,Name,EmailAddress |
Select-Object Displayname,Name,EmailAddress |
Export-Excel -Path 'C:\temp\XXXX-2023.xlsx' -WorkSheetname $WorksheetName -AutoSize -title "ADGroup: $group" -TitleBold
}
Cheers,
Lain