Skip to content

Instantly share code, notes, and snippets.

@joegasper
Created October 31, 2023 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joegasper/c320fa8a76c4fc3ecef2fcd9b860d61a to your computer and use it in GitHub Desktop.
Save joegasper/c320fa8a76c4fc3ecef2fcd9b860d61a to your computer and use it in GitHub Desktop.
Microsoft 365 Group memberships for a user or summary report using ActiveDirectory module
<#
.Synopsis
Microsoft 365 Group memberships for a user or summary report
.DESCRIPTION
Output a list of a user's modern group memberships (Teams, Engage, Planner, Groups) or summary report
.EXAMPLE
Get-M365GroupsForUser -UserName pvenkman | sort DisplayName | select DisplayName,Description
.EXAMPLE
Get-M365GroupsForUser -UserName pvenkman -Report | where Owners -like "*rsantz*"
.EXAMPLE
Get-M365GroupsForUser -UserName pvenkman | select DisplayName,Created,Description | Export-Csv -Path .\Grps.csv -NoTypeInformation
.EXAMPLE
Get-M365GroupsForUser -UserName pvenkman -Report | Format-Table
.EXAMPLE
Get-M365GroupsForUser -UserName pvenkman -Report | Export-Csv -Path .\GrpsReport.csv -NoTypeInformation
#>
function Get-M365GroupsForUser
{
[CmdletBinding()]
[Alias()]
[OutputType([string])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$UserName,
[switch]$Report
)
Begin
{
class InfoObject {
[string]$DisplayName
[string]$ShortName
[string]$GroupId
[datetime]$Created
[int]$NumMembers
[string]$Owners
[string]$Description
InfoObject($DisplayName, $ShortName, $GroupId, $Created, $NumMembers, $Owners, $Description) {
$this.DisplayName = $DisplayName
$this.ShortName = $ShortName
$this.GroupId = $GroupId
$this.Created = $Created
$this.NumMembers = $NumMembers
$this.Owners = $Owners
$this.Description = $Description
}
}
}
Process
{
$Groups = ((Get-ADUser -Properties MemberOf -Identity $UserName).MemberOf).Where{$_ -match 'CN=Group_'}
foreach ($Group in $Groups) {
if ($Report.IsPresent) {
$OwnersNames = @()
$RptGrp = Get-ADGroup $Group -Properties DisplayName,mailNickname,Name,Created,msExchGroupMemberCount,msExchCoManagedByLink,Description
if ($RptGrp.msExchCoManagedByLink) {
$OwnersFQ = $RptGrp.msExchCoManagedByLink
$OwnersNames = foreach ($OwnerListed in $OwnersFQ) {
(Get-ADUser -Identity $OwnerListed).Name
}
}
$col1 = $RptGrp.DisplayName
$col2 = $RptGrp.MailNickName
$col3 = $RptGrp.Name.Replace('Group_','')
$col4 = $RptGrp.Created
$col5 = $RptGrp.msExchGroupMemberCount
$col6 = $OwnersNames -join ';'
$col7 = $RptGrp.Description
[InfoObject]::new($col1, $col2, $col3, $col4, $col5, $col6, $col7)
} else {
Get-ADGroup $Group -Properties *
}
}
}
End
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment