Sinun on annettava käyttäjälle samat ryhmäjäsenyydet kuin toiselle käyttäjälle. Voit tehdä tämän manuaalisesti lisäämällä jokainen ryhmä erikseen käyttäjälle. Mutta se vie aikaa. Parempi tapa on automatisoida prosessi PowerShellillä. Tässä artikkelissa opit kopioimaan ryhmän jäsenyyden käyttäjältä toiselle Microsoft Entra ID:ssä.
Kopioi ryhmän jäsenyys PowerShell-skripti
Copy-GroupMembership.ps1 PowerShell-komentosarja kopioi tietyn käyttäjän (lähteen) ryhmäjäsenyydet toiselle käyttäjälle (kohde) Microsoft Entra ID:ssä.
Huomautus:Komentosarja ei voi kopioida postia tukevia suojausryhmiä tai jakeluluetteloita, koska niitä on hallittava Exchange Online PowerShellin avulla. Lisäksi, jos ryhmä synkronoidaan paikallisesta Active Directorysta, voit hallita sitä vain sieltä, et Microsoft Entra ID:stä.
Tässä on esimerkki käyttäjän ryhmäjäsenyydestä[sähköposti suojattu].

Vaihe 1. Asenna Microsoft Graph PowerShell
Suorita Windows PowerShell järjestelmänvalvojana ja asenna Microsoft Graph PowerShell.
Install-Module Microsoft.Graph -ForceTärkeää:Päivitä aina uusimpaan Microsoft Graph PowerShell -moduuliversioon ennen kuin suoritat cmdletin tai komentosarjan virheiden ja virheellisten tulosten estämiseksi.
Nyt kun Microsoft Graph PowerShell SDK -moduuli on asennettu, voimme siirtyä seuraavaan vaiheeseen.
Vaihe 2. Yhdistä Microsoft Graph PowerShelliin
Sinun on muodostettava yhteys Microsoft Graph PowerShelliin oikeilla käyttöoikeuksilla.
Connect-MgGraph -Scopes "User.Read.All", "Group.Read.All", "GroupMember.ReadWrite.All" -NoWelcomeVaihe 3. Lataa Copy-Membership PowerShell-komentosarja
Luo kaksi kansiota(C:)ajaa:
- Käsikirjoitukset
- Temp
Lataa ja aseta Copy-GroupMembership.ps1 PowerShell-komentosarja tiedostoonC: Scriptskansio. Komentosarja kopioi kaikki tietyn käyttäjän ryhmäjäsenyydet toiselle käyttäjälle Microsoft Entra ID:ssä.
Varmista, että tiedoston esto on poistettu, jotta estetään virheet komentosarjaa suoritettaessa. Lue lisää artikkelista Ei digitaalisesti allekirjoitettu virhe PowerShell-skriptiä suoritettaessa.
Toinen vaihtoehto on kopioida ja liittää alla oleva koodi Muistioon. Anna sille nimiCopy-GroupMemberships.ps1ja aseta se sisäänC: Scriptskansio.
<#
.SYNOPSIS
Copy-GroupMemberships.ps1
.DESCRIPTION
Copy all group memberships of a specified user to another user in Microsoft Entra ID.
.LINK
www.alitajran.com/copy-group-membership-from-one-user-to-another-in-microsoft-entra-id/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
X: x.com/alitajran
.CHANGELOG
V1.00, 05/25/2025 - Initial version
#>
# Define parameters for the script
param (
[Parameter(Mandatory = $true, HelpMessage = "Enter the User ID (e.g., email or object ID) of the source Entra ID user")]
[string]$UserId,
[Parameter(HelpMessage = "Specify the path for the CSV output file")]
[string]$CsvFilePath,
[Parameter(HelpMessage = "Enable to display results in Out-GridView")]
[switch]$OutGridView,
[Parameter(HelpMessage = "Enter the User ID (e.g., email or object ID) of the target user to copy memberships to")]
[string]$TargetUserId
)
# Connect to Microsoft Graph with required scopes
Connect-MgGraph -Scopes "User.Read.All", "Group.Read.All", "GroupMember.ReadWrite.All" -NoWelcome
# Initialize a list to store report data
$Report = [System.Collections.Generic.List[Object]]::new()
try {
# If TargetUserId is provided, verify target user exists
if ($TargetUserId) {
$TargetUser = Get-MgUser -UserId $TargetUserId -ErrorAction Stop
}
# Fetch the source user's group memberships
$EntraGroupMembers = Get-MgUserMemberOf -UserId $UserId -All -ErrorAction Stop
# Check if the source user is a member of any groups
if (-not $EntraGroupMembers) {
Write-Host "No group memberships found for user: $UserId" -ForegroundColor Cyan
return
}
# Process each group membership
foreach ($EntraGroup in $EntraGroupMembers) {
# Extract group details from AdditionalProperties
$AdditionalProperties = $EntraGroup.AdditionalProperties
# Determine group type
$GroupType = if ($AdditionalProperties.groupTypes -contains "Unified" -and $AdditionalProperties.securityEnabled) {
"Microsoft 365 (security-enabled)"
}
elseif ($AdditionalProperties.groupTypes -contains "Unified" -and -not $AdditionalProperties.securityEnabled) {
"Microsoft 365"
}
elseif (-not ($AdditionalProperties.groupTypes -contains "Unified") -and $AdditionalProperties.securityEnabled -and $AdditionalProperties.mailEnabled) {
"Mail-enabled security"
}
elseif (-not ($AdditionalProperties.groupTypes -contains "Unified") -and $AdditionalProperties.securityEnabled) {
"Security"
}
elseif (-not ($AdditionalProperties.groupTypes -contains "Unified") -and $AdditionalProperties.mailEnabled) {
"Distribution"
}
else {
"N/A"
}
# Create a custom object for the group details
$GroupDetails = [PSCustomObject]@{
Id = $EntraGroup.Id
DisplayName = if ($AdditionalProperties.displayName) { $AdditionalProperties.displayName } else { "N/A" }
Email = if ($AdditionalProperties.mail) { $AdditionalProperties.mail } else { "N/A" }
SecurityEnabled = if ($AdditionalProperties.securityEnabled) { $AdditionalProperties.securityEnabled } else { "N/A" }
MailEnabled = if ($AdditionalProperties.mailEnabled) { $AdditionalProperties.mailEnabled } else { "N/A" }
GroupType = $GroupType
Source = if ($AdditionalProperties.onPremisesSyncEnabled) { "On-Premises" } else { "Cloud" }
}
# Add the group details to the report list
$Report.Add($GroupDetails)
}
# Output to console only if TargetUserId is not provided
if (-not $TargetUserId) {
$Report | Sort-Object DisplayName | Format-Table -AutoSize
}
# Output to Out-GridView if specified
if ($OutGridView) {
$Report | Sort-Object DisplayName | Out-GridView -Title "Group Memberships for $UserId"
}
# Export to CSV if CsvFilePath is provided
if ($CsvFilePath) {
$Report | Sort-Object DisplayName | Export-Csv -Path $CsvFilePath -NoTypeInformation -Force
Write-Host "Group memberships exported to $CsvFilePath" -ForegroundColor Cyan
}
# Copy memberships to target user if TargetUserId is provided
if ($TargetUserId) {
foreach ($Group in $Report) {
try {
# Check if the target user is already a member
$ExistingMember = Get-MgGroupMember -GroupId $Group.Id -All | Where-Object { $_.Id -eq $TargetUser.Id }
if (-not $ExistingMember) {
New-MgGroupMember -GroupId $Group.Id -DirectoryObjectId $TargetUser.Id -ErrorAction Stop
Write-Host "Added $TargetUserId to group: $($Group.DisplayName)" -ForegroundColor Green
}
else {
Write-Host "User $TargetUserId is already a member of group: $($Group.DisplayName)" -ForegroundColor Yellow
}
}
catch {
Write-Host "Failed to add $TargetUserId to group $($Group.DisplayName): $($_.Exception.Message)" -ForegroundColor Red
}
}
}
}
catch {
# Handle errors (e.g., invalid UserId, insufficient permissions, or Graph API issues)
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
}Vaihe 4. Suorita Copy-GroupMembership PowerShell-komentosarja
Suorita alla oleva komento saadaksesi käyttäjän ryhmäjäsenyyden.
C:scripts.Copy-GroupMemberships.ps1 -UserId "[email protected]"Suorita alla oleva komento saadaksesi käyttäjän ryhmäjäsenyyden ja näytä tulos interaktiivisessa taulukossa erillisessä ikkunassa.
C:scripts.Copy-GroupMemberships.ps1 -UserId "[email protected]" -OutGridViewSuorita alla oleva komento saadaksesi käyttäjän ryhmäjäsenyyden, näytä tulos interaktiivisessa taulukossa erillisessä ikkunassa ja vie tulokset CSV-tiedostoon.
C:scripts.Copy-GroupMemberships.ps1 -UserId "[email protected]" -OutGridView -CsvFilePath "C:tempAmanda_GroupMemberships.csv"Tulos näkyy aina PowerShellissä.
Id DisplayName Email SecurityEnabled MailEnabled GroupType Source
-- ----------- ----- --------------- ----------- --------- ------
d62bbb93-90d6-4560-94f5-7536cb1d5ac5 All Company [email protected] N/A True Microsoft 365 Cloud
c36abb7f-30e6-4cf5-9c12-12a4b8184d96 DG001 [email protected] N/A True Distribution Cloud
068346f3-3b9a-44b8-81ff-db5167644d74 Group1_WR N/A True N/A Security Cloud
496c0a4a-91a0-4c5c-bbd3-b4bb30211e56 HR N/A True N/A Security Cloud
7def1c3e-ccbe-4458-ac41-8e9452460e9a Sales [email protected] N/A True Microsoft 365 CloudKopioi lähdekäyttäjän ryhmän jäsenet ([sähköposti suojattu]) kohdekäyttäjälle ([sähköposti suojattu]).
Suositeltu luettava:Ryhmäkirjoituksen ottaminen käyttöön Microsoft Entra Connect Syncissä
C:scripts.Copy-GroupMemberships.ps1 -UserId "[email protected]" -TargetUserId "[email protected]"Tulos tulee näkyviin.
Added [email protected] to group: Group1_WR
Added [email protected] to group: All Company
User [email protected] is already a member of group: Sales
Failed to add [email protected] to group DG001: [Request_BadRequest] : Cannot Update a mail-enabled security groups and or distribution list.
Added [email protected] to group: HRVarmista, että ryhmäjäsenyydet on kopioitu onnistuneesti.
C:scripts.Copy-GroupMemberships.ps1 -UserId "[email protected]" -OutGridView -CsvFilePath "C:tempElisa_GroupMemberships.csv"Tältä kohdekäyttäjä Elisa Malorin ryhmäjäsenyys näyttää.

siinä se!
Johtopäätös
Opit kopioimaan ryhmän jäsenyyden käyttäjältä toiselle Microsoft Entra ID:ssä. Käytä PowerShell-komentosarjaa saadaksesi käyttäjän (lähde) ryhmäjäsenyyden. Kopioi se sitten toiselle käyttäjälle (kohde). Varmista, että ryhmän jäsenyys on lisätty kohdekäyttäjään onnistuneesti.
Piditkö tästä artikkelista? Saatat myös pitää Get-MgUserin käyttämisestä PowerShellissä. Älä unohda seurata meitä ja jakaa tätä artikkelia.








![[Vinkki] Kuinka poistaa mustan reunuksen ääriviivat, kun napsautat tekstinsyöttöruutua Google Chromessa](https://media.askvg.com/articles/images8/Chrome_Shows_Black_Border_Outline_Highlighting_Focused_Text_Input_Boxes.png)

