Forum Discussion

My-atix's avatar
My-atix
Copper Contributor
Jul 17, 2023
Solved

Help generating CSV correctly...

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 m...
  • LeonPavesic's avatar
    Jul 17, 2023

    Hi 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

Resources