mirror of
https://github.com/admindroid-community/powershell-scripts.git
synced 2025-12-17 16:35:19 +00:00
Export Microsoft 365 Groups a User is Member Of
Export Microsoft 365 Groups a User is Member Of
This commit is contained in:
parent
0c74ffa0ff
commit
31d591c3fb
@ -1,9 +1,24 @@
|
||||
<#
|
||||
<#
|
||||
=============================================================================================
|
||||
Name: Office365 User Membership Report Using PowerShell
|
||||
Description: This script exports Office 365 user's group details to CSV
|
||||
Version: 3.0
|
||||
Website: o365reports.com
|
||||
|
||||
Script Highlights :
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
1. Generates 12 different user membership reports.
|
||||
2. The script uses MS Graph PowerShell and installs MS Graph PowerShell SDK (if not installed already) upon your confirmation.
|
||||
3. It can be executed with certificate-based authentication (CBA) too.
|
||||
4. Supports both MFA and Non-MFA accounts.
|
||||
5. Allow to use filter to get guest users and their membership alone.
|
||||
6. Allow to use filter to get disabled users’ membership.
|
||||
7. Helps to identify users who are not member of any groups.
|
||||
8. Exports report result to CSV.
|
||||
9. The script is scheduler-friendly.
|
||||
|
||||
|
||||
For detailed script execution:https://o365reports.com/2021/04/15/export-office-365-groups-a-user-is-member-of-using-powershell
|
||||
============================================================================================
|
||||
#>
|
||||
@ -18,20 +33,20 @@ param
|
||||
[string] $ClientId,
|
||||
[string] $CertificateThumbprint
|
||||
)
|
||||
$MsGraphModule = Get-Module Microsoft.Graph -ListAvailable
|
||||
if($MsGraphModule -eq $null)
|
||||
$MsGraphBetaModule = Get-Module Microsoft.Graph.Beta -ListAvailable
|
||||
if($MsGraphBetaModule -eq $null)
|
||||
{
|
||||
Write-host "Important: Microsoft graph module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
|
||||
$confirm = Read-Host Are you sure you want to install Microsoft graph module? [Y] Yes [N] No
|
||||
Write-host "Important: Microsoft Graph Beta module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
|
||||
$confirm = Read-Host Are you sure you want to install Microsoft Graph Beta module? [Y] Yes [N] No
|
||||
if($confirm -match "[yY]")
|
||||
{
|
||||
Write-host "Installing Microsoft graph module..."
|
||||
Install-Module Microsoft.Graph -Scope CurrentUser
|
||||
Write-host "Microsoft graph module is installed in the machine successfully" -ForegroundColor Magenta
|
||||
Write-host "Installing Microsoft Graph Beta module..."
|
||||
Install-Module Microsoft.Graph.Beta -Scope CurrentUser -AllowClobber
|
||||
Write-host "Microsoft Graph Beta module is installed in the machine successfully" -ForegroundColor Magenta
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-host "Exiting. `nNote: Microsoft graph module must be available in your system to run the script" -ForegroundColor Red
|
||||
Write-host "Exiting. `nNote: Microsoft Graph Beta module must be available in your system to run the script" -ForegroundColor Red
|
||||
Exit
|
||||
}
|
||||
}
|
||||
@ -53,8 +68,9 @@ else
|
||||
Exit
|
||||
}
|
||||
}
|
||||
Write-Host "Microsoft Graph Powershell module is connected successfully" -ForegroundColor Green
|
||||
Select-MgProfile beta
|
||||
Write-Host "Microsoft Graph Beta PowerShell module is connected successfully" -ForegroundColor Green
|
||||
Write-Host "`nNote: If you encounter module related conflicts, run the script in a fresh PowerShell window."
|
||||
|
||||
Function UserDetails {
|
||||
if ([string]$UsersIdentityFile -ne "")
|
||||
{
|
||||
@ -64,7 +80,7 @@ Function UserDetails {
|
||||
$CurIdentity = $IdentityValue.UserIdentityValue
|
||||
try
|
||||
{
|
||||
$LiveUser = Get-MgUser -UserId "$CurIdentity" -ExpandProperty MemberOf -ErrorAction SilentlyContinue
|
||||
$LiveUser = Get-MgBetaUser -UserId "$CurIdentity" -ExpandProperty MemberOf -ErrorAction SilentlyContinue
|
||||
if($GuestUsersOnly.IsPresent -and $LiveUser.UserType -ne "Guest")
|
||||
{
|
||||
continue
|
||||
@ -85,28 +101,28 @@ Function UserDetails {
|
||||
{
|
||||
if ($GuestUsersOnly.Ispresent -and $DisabledUsersOnly.Ispresent)
|
||||
{
|
||||
Get-MgUser -Filter "UserType eq 'Guest'" -ExpandProperty MemberOf -All| Where-Object { $_.AccountEnabled -eq $false } | ForEach-Object {
|
||||
Get-MgBetaUser -Filter "UserType eq 'Guest'" -ExpandProperty MemberOf -All| Where-Object { $_.AccountEnabled -eq $false } | ForEach-Object {
|
||||
$LiveUser = $_
|
||||
ProcessUser
|
||||
}
|
||||
}
|
||||
elseif ($DisabledUsersOnly.Ispresent)
|
||||
{
|
||||
Get-MgUser -ExpandProperty MemberOf -All| Where-Object { $_.AccountEnabled -eq $false } | ForEach-Object {
|
||||
Get-MgBetaUser -ExpandProperty MemberOf -All| Where-Object { $_.AccountEnabled -eq $false } | ForEach-Object {
|
||||
$LiveUser = $_
|
||||
ProcessUser
|
||||
}
|
||||
}
|
||||
elseif ($GuestUsersOnly.Ispresent)
|
||||
{
|
||||
Get-MgUser -Filter "UserType eq 'Guest'" -ExpandProperty MemberOf -All| ForEach-Object {
|
||||
Get-MgBetaUser -Filter "UserType eq 'Guest'" -ExpandProperty MemberOf -All| ForEach-Object {
|
||||
$LiveUser = $_
|
||||
ProcessUser
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Get-MgUser -ExpandProperty MemberOf -All | ForEach-Object {
|
||||
Get-MgBetaUser -ExpandProperty MemberOf -All | ForEach-Object {
|
||||
$LiveUser = $_
|
||||
ProcessUser
|
||||
}
|
||||
@ -119,7 +135,7 @@ Function ProcessUser {
|
||||
$Script:ProcessedUsers += 1
|
||||
$Name = $LiveUser.DisplayName
|
||||
Write-Progress -Activity "Processing $Name" -Status "Processed Users Count: $Script:ProcessedUsers"
|
||||
$UserMembership = Get-MgUserMemberOf -UserId $LiveUser.UserPrincipalName |Select-Object -ExpandProperty AdditionalProperties
|
||||
$UserMembership = Get-MgBetaUserTransitiveMemberOf -UserId $LiveUser.UserPrincipalName |Select-Object -ExpandProperty AdditionalProperties
|
||||
$AllGroupData = $UserMembership | Where-object { $_.'@odata.type' -eq "#microsoft.graph.group" }
|
||||
if ($AllGroupData -eq $null)
|
||||
{
|
||||
@ -182,7 +198,7 @@ if ((Test-Path -Path $ExportCSVFileName) -eq "True") {
|
||||
Write-Host "`nThe Output result has " -NoNewline
|
||||
Write-Host $Script:ExportedUsers Users -ForegroundColor Magenta -NoNewline
|
||||
Write-Host " details"
|
||||
Write-Host `nThe Output file available in $ExportCSVFileName -ForegroundColor Green
|
||||
Write-Host `nThe Output file available in -NoNewline -Foregroundcolor Yellow; Write-Host $ExportCSVFileName
|
||||
$prompt = New-Object -ComObject wscript.shell
|
||||
$userInput = $prompt.popup("Do you want to open output file?", 0, "Open Output File", 4)
|
||||
if ($userInput -eq 6) {
|
||||
@ -191,5 +207,7 @@ if ((Test-Path -Path $ExportCSVFileName) -eq "True") {
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Host `nNo data/user found with the specified criteria -ForegroundColor Red
|
||||
}
|
||||
Write-Host `nNo data/user found with the specified criteria
|
||||
}
|
||||
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
|
||||
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
|
||||
Loading…
x
Reference in New Issue
Block a user