Created
October 31, 2023 02:10
-
-
Save joegasper/783a88d473354089c32620c8a79eb826 to your computer and use it in GitHub Desktop.
Display Microsoft 365 Groups owned by a user using the ActiveDirectory module and msExchCoManagedByLink attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Display Microsoft 365 Groups owned by a user. | |
.DESCRIPTION | |
Display modern groups (Microsoft 365 Group, Microsoft Teams, Planner, Viva Engage, etc.) | |
of which the user is an owner. Script requires synchronization of modern groups to the | |
operator's Active Directory and the operator has permissions to read user object attributes. | |
The script requires the PowerShell module "ActiveDirectory". | |
.EXAMPLE | |
Get-M365GroupsOwned -UserName pvenkman | select DisplayName,Name | |
DisplayName Name | |
----------- ---- | |
Demo-Staff-Team Group_9f695aaf-84fc-419d-ad4b-2873be13e74a | |
Research Symposium 2021 Group_67909e5b-3f72-411f-84b4-e82581dd3d0f | |
Tree Pop Model Group_6ebbf55d-3e8e-46c6-93cc-08d765005f30 | |
JUGA 2021 Group_98ec65bd-6585-490f-a13b-5cd5c3e2490b | |
JUGA 2020 Group_435ff4c5-03fb-4c6b-8641-9bd0b952f971 | |
DEPT-JUGA-2019 Group_03512c72-4539-4f3d-8158-d631a2cf2f3a | |
Get modern groups owned by the user. | |
.EXAMPLE | |
Get-M365GroupsOwned -UserName pvenkman | Where-Object DisplayName -like '*juga*' | Select-Object DisplayName,Name | |
DisplayName Name | |
----------- ---- | |
JUGA 2021 Group_98ec65bd-6585-490f-a13b-5cd5c3e2490b | |
JUGA 2020 Group_435ff4c5-03fb-4c6b-8641-9bd0b952f971 | |
DEPT-JUGA-2019 Group_03512c72-4539-4f3d-8158-d631a2cf2f3a | |
Display modern groups owned by the user with specific string in the DisplayName. | |
.INPUTS | |
Username | |
.OUTPUTS | |
Groups | |
#> | |
function Get-M365GroupsOwned | |
{ | |
[CmdletBinding(SupportsShouldProcess=$true, | |
PositionalBinding=$true)] | |
[Alias()] | |
[OutputType([String])] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
ValueFromRemainingArguments=$false, | |
Position=0)] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
$UserName | |
) | |
Begin { | |
} | |
Process | |
{ | |
if ($pscmdlet.ShouldProcess("Target", "Operation")) { | |
$User = Get-ADUser -Identity $UserName -Properties MemberOf | |
foreach ( $Group in $User.MemberOf ) { | |
if ($Group -like 'CN=Group_*') { | |
$GroupObj = Get-ADGroup -Identity $Group -Properties * | |
if ($GroupObj.msExchCoManagedByLink) { | |
if ( $GroupObj.msExchCoManagedByLink.Contains($User.DistinguishedName) ) { | |
$GroupObj | |
} | |
} | |
} | |
} | |
} | |
} | |
End { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment