Skip to content

Instantly share code, notes, and snippets.

@J0rtIT
Last active June 4, 2022 02:03
Show Gist options
  • Save J0rtIT/83c008cb70706507e84033b8cb5c6225 to your computer and use it in GitHub Desktop.
Save J0rtIT/83c008cb70706507e84033b8cb5c6225 to your computer and use it in GitHub Desktop.
Script to gather Groups membership by user in Office365
<#
.Synopsis
Gather groups membership by user in all O365 Tenant.
Medium Article: https://j0rt3g4.medium.com/report-with-all-the-group-membership-by-the-user-in-office-365-ca48a2398072
.DESCRIPTION
Gather the membership by user in all O365 Groups
.EXAMPLE
.\Get-GroupsMemberByUser.ps1
Get the membership saved into Variable $Report and show it on screen
.EXAMPLE
.\Get-GroupsMemberByUser.ps1 -CSV
Get the membership saved into Variable $Report and export it into the Desktop of the current logged user with the given name: DEfault Name GroupsByUsers.csv
.INPUTS
Any of the following sets:
Set1:
-CSV Switch parameter (true or false)
-Filename Output filename on Desktop
Set2: Default
-Screen to gather the results on screen
.OUTPUTS
Screen or CSV File
.NOTES
Jose Ortega - https://j0rt3g4.medium.com
.COMPONENT
The component this cmdlet was a solution for a ticket.
.ROLE
This CMdlet doesn't have any role
.FUNCTIONALITY
Report on Screen or CSV the group membership in an O365 tenant
#>
[Cmdletbinding(DefaultParameterSetName="Default")]
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true,Position=0,ParameterSetName="File")][switch]$CSV=$true,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,Position=1,ParameterSetName="File")]$FileName="GroupsByUsers.csv",
[Parameter(Mandatory=$false,ValueFromPipeline=$true,Position=0,ParameterSetName="Default")][switch]$screen=$true
)
#connect or install
try{
Get-MsolUser -ea Stop | select -First 1 | Out-Null
}
catch{
try{
Connect-MsolService -ea Stop | Out-Null
}
catch{
Install-Module MSOnline
Connect-MsolService
}
}
#functions
function IsAMember{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]$user,
[Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)]$group
)
foreach($member in $(Get-MsolGroupMember -GroupObjectId $group.ObjectId)){
if($user -eq $member.emailAddress){
return $true
}
}
}
#Final Report Variable
$Report = @()
#Gather all users in Tenant - Except the Guests ones (#EXT#)
$AllUsers = Get-MsolUser | where{ $_.UserPrincipalName -notlike "*#EXT#*"} | select -ExpandProperty USerPrincipalName
#Gather All groupps in tenant
$Allgroups=Get-MsolGroup | select ObjectId,DisplayName
Write-Progress -Activity "Creating Report" -PercentComplete 0 -Id 1
$i=0;
foreach($email in $AllUsers){
$i++
$x =($i/ $AllUsers.Count) * 100
Write-Progress -Activity "Creating Report" -PercentComplete $x -Id 1
Write-Progress -Activity "Working with User $email" -PercentComplete 25 -ParentId 1
Write-Host -ForegroundColor Gray "Working with user $email"
#cleanupgroups
$groups=@()
foreach($g in $Allgroups){
if(IsAMember -user $email -group $g){
$groups+= $g.DisplayName
}
}
Write-Host -ForegroundColor Green "Finished with user $email"
Write-Progress -Activity "Working with User $email" -PercentComplete 100 -ParentId 1 -Completed
$Report+= New-Object -TypeName PSObject -Property @{"User" = $email; "GroupMembership" = $groups -join ';'}
}
Write-Progress -Activity "Creating Report" -PercentComplete 100 -Id 0 -Completed
#OnScreen
if($screen){
$Report
}
if ($CSV){
$Report | Export-Csv -Path "$env:userprofile\desktop\$FileName" -NoTypeInformation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment