Jul 17 2023 01:51 AM
Hi All,
I am having issues creating a CSV file that puts the commas in the right place?
I have the following script that creates a report of M365 groups, however when I try to get the group members of each group then the results are not comma separated???
# set the name of the report alongside the path ".\ relative path"
$ReportName = ".\M365GroupReport.csv"
# get office 365 group
$GroupDetails = Get-UnifiedGroup -Filter {ResourceProvisioningOptions -eq "Team"} -ResultSize Unlimited | Select-Object PrimarySmtpAddress, DisplayName, ManagedByDetails, `
WhenCreatedUTC, AccessType, GroupMemberCount, GroupExternalMemberCount, IsMembershipDynamic
# set export array
$ExportArray = @()
# loop through each group
Foreach ($GroupDetail in $GroupDetails) {
# set the group primary smtp address
$GroupUpn = $GroupDetail.PrimarySmtpAddress.ToString()
# get office 365 group members
$GroupMembersDetails = Get-UnifiedGroupLinks -ResultSize Unlimited -Identity $GroupUpn -LinkType Members `
| Select-Object Name, PrimarySmtpAddress
# add values to array
$ExportArray += [PSCustomObject][Ordered]@{
# get office 365 group display name
"Group Display Name" = $GroupDetail.DisplayName.ToString()
# get office 365 group primary smtp address
"Group PrimarySmtpAddress" = $GroupUpn
# get office 365 group creation date
"When Created (UTC)" = $GroupDetail.WhenCreatedUTC.ToString()
# get office 365 privacy status
"Privacy Status" = $GroupDetail.AccessType.ToString()
# get office 365 group member count
"Group Member Count" = $GroupDetail.GroupMemberCount.ToString()
# get office 365 group external member count
"Group External Member Count" = $GroupDetail.GroupExternalMemberCount.ToString()
# get dynamic memberShip status
"Dynamic MemberShip Status" = $GroupDetail.IsMembershipDynamic.ToString()
# get office 365 group owner(s)
"Owners" = ($GroupDetail.ManagedByDetails | Out-String).Trim()
# get the display name of the members
"Display Name of Members" = ($GroupMembersDetails.Name | Out-String).Trim()
# get the primary smtp address of the members
"Members' PrimarySmtpAddress" = ($GroupMembersDetails.PrimarySmtpAddress | Out-String).Trim()
}
}
# export to csv file
$ExportArray | Export-Csv -Path $ReportName -Delimiter "," -NoTypeInformation
How do I make sure that the group members are also returned with a comma and not just the group properties???
Thanks for your assistance.
Jul 17 2023 03:19 AM - edited Jul 17 2023 03:27 AM
Hi @My-atix
Your $GroupMembersDetails variable will be an Object[]
$GroupMembersDetails.GetType()
One method would be to join the members together using:
$GroupMembersDetails.Name -join ';'
The above example uses a semicolon to join the members, but you can replace this with something that suits your requirements
Jul 17 2023 03:20 AM
SolutionHi @My-atix,
i tried to correct (change) your script. Change the path at the start of the script to the path you want (and you have permission to).
# Set the name of the report alongside the path
$ReportName = "C:\Temp\M365GroupReport.csv"
# Get Office 365 groups
$GroupDetails = Get-UnifiedGroup -Filter {ResourceProvisioningOptions -eq "Team"} -ResultSize Unlimited |
Select-Object PrimarySmtpAddress, DisplayName, ManagedByDetails, WhenCreatedUTC, AccessType, GroupMemberCount, GroupExternalMemberCount, IsMembershipDynamic
# Set export array
$ExportArray = @()
# Loop through each group
foreach ($GroupDetail in $GroupDetails) {
# Set the group primary SMTP address
$GroupUpn = $GroupDetail.PrimarySmtpAddress.ToString()
# Get Office 365 group members
$GroupMembersDetails = Get-UnifiedGroupLinks -ResultSize Unlimited -Identity $GroupUpn -LinkType Members |
Select-Object Name, PrimarySmtpAddress |
ForEach-Object {
$_.Name = ($_.Name -join ",")
$_.PrimarySmtpAddress = ($_.PrimarySmtpAddress -join ",")
$_
}
# Add values to array
$ExportArray += [PSCustomObject] [Ordered] @{
# Get Office 365 group display name
"Group Display Name" = $GroupDetail.DisplayName.ToString()
# Get Office 365 group primary SMTP address
"Group PrimarySmtpAddress" = $GroupUpn
# Get Office 365 group creation date
"When Created (UTC)" = $GroupDetail.WhenCreatedUTC.ToString()
# Get Office 365 privacy status
"Privacy Status" = $GroupDetail.AccessType.ToString()
# Get Office 365 group member count
"Group Member Count" = $GroupDetail.GroupMemberCount.ToString()
# Get Office 365 group external member count
"Group External Member Count" = $GroupDetail.GroupExternalMemberCount.ToString()
# Get dynamic membership status
"Dynamic Membership Status" = $GroupDetail.IsMembershipDynamic.ToString()
# Get Office 365 group owner(s)
"Owners" = ($GroupDetail.ManagedByDetails | Out-String).Trim()
# Get the display name of the members
"Display Name of Members" = $GroupMembersDetails.Name -join ","
# Get the primary SMTP address of the members
"Members' PrimarySmtpAddress" = $GroupMembersDetails.PrimarySmtpAddress -join ","
}
}
# Export to CSV file
$ExportArray | Export-Csv -Path $ReportName -Delimiter "," -NoTypeInformation
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
Jul 17 2023 03:20 AM
SolutionHi @My-atix,
i tried to correct (change) your script. Change the path at the start of the script to the path you want (and you have permission to).
# Set the name of the report alongside the path
$ReportName = "C:\Temp\M365GroupReport.csv"
# Get Office 365 groups
$GroupDetails = Get-UnifiedGroup -Filter {ResourceProvisioningOptions -eq "Team"} -ResultSize Unlimited |
Select-Object PrimarySmtpAddress, DisplayName, ManagedByDetails, WhenCreatedUTC, AccessType, GroupMemberCount, GroupExternalMemberCount, IsMembershipDynamic
# Set export array
$ExportArray = @()
# Loop through each group
foreach ($GroupDetail in $GroupDetails) {
# Set the group primary SMTP address
$GroupUpn = $GroupDetail.PrimarySmtpAddress.ToString()
# Get Office 365 group members
$GroupMembersDetails = Get-UnifiedGroupLinks -ResultSize Unlimited -Identity $GroupUpn -LinkType Members |
Select-Object Name, PrimarySmtpAddress |
ForEach-Object {
$_.Name = ($_.Name -join ",")
$_.PrimarySmtpAddress = ($_.PrimarySmtpAddress -join ",")
$_
}
# Add values to array
$ExportArray += [PSCustomObject] [Ordered] @{
# Get Office 365 group display name
"Group Display Name" = $GroupDetail.DisplayName.ToString()
# Get Office 365 group primary SMTP address
"Group PrimarySmtpAddress" = $GroupUpn
# Get Office 365 group creation date
"When Created (UTC)" = $GroupDetail.WhenCreatedUTC.ToString()
# Get Office 365 privacy status
"Privacy Status" = $GroupDetail.AccessType.ToString()
# Get Office 365 group member count
"Group Member Count" = $GroupDetail.GroupMemberCount.ToString()
# Get Office 365 group external member count
"Group External Member Count" = $GroupDetail.GroupExternalMemberCount.ToString()
# Get dynamic membership status
"Dynamic Membership Status" = $GroupDetail.IsMembershipDynamic.ToString()
# Get Office 365 group owner(s)
"Owners" = ($GroupDetail.ManagedByDetails | Out-String).Trim()
# Get the display name of the members
"Display Name of Members" = $GroupMembersDetails.Name -join ","
# Get the primary SMTP address of the members
"Members' PrimarySmtpAddress" = $GroupMembersDetails.PrimarySmtpAddress -join ","
}
}
# Export to CSV file
$ExportArray | Export-Csv -Path $ReportName -Delimiter "," -NoTypeInformation
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic