{"id":6124,"date":"2022-09-06T11:01:51","date_gmt":"2022-09-06T11:01:51","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=6124"},"modified":"2022-11-23T00:57:18","modified_gmt":"2022-11-23T00:57:18","slug":"copy-azure-ad-security-groups-and-membership-one-tenancy-to-another","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/copy-azure-ad-security-groups-and-membership-one-tenancy-to-another","title":{"rendered":"Copy Azure AD Security Groups and Membership one Tenancy to Another"},"content":{"rendered":"<div class=\"wp-block-wab-pastacode\">\n\t<div class=\"code-embed-wrapper\"> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">#Sec Groups list in array &quot;1&quot;,&quot;2&quot;<br\/>$SG = &quot;GroupName&quot;<br\/><br\/>#To Grab All<br\/>#$SG = Get-AzureADGroup -All $true | Select DisplayName<br\/><br\/><br\/>$ZSourceDomain = &quot;sourcedomain.com&quot;<br\/>$CSourceDomain = &quot;sourcedomain2.com&quot;<br\/>$DestinationDomain = &quot;destinationdomain3.com&quot;<br\/><br\/>foreach($S in $SG){<br\/><br\/>#connect to source tenant , have to save creds if we are working with a lot of groups if not comment out $zpassword and $zpassword\u00a0<br\/>#https:\/\/sid-500.com\/2020\/12\/01\/powershell-connect-to-azure-with-stored-credentials\/<br\/>$zpassword = ConvertTo-SecureString (Unprotect-CmsMessage -Path C:\\Scripts\\pwd.txt) -AsPlainText -Force<br\/>$zcred= New-Object System.Management.Automation.PSCredential (&#039;XXXXXX&#039;, $zpassword)<br\/>Connect-AzureAD -credential $zcred<br\/><br\/><br\/>$List = @()<br\/>$List = Get-AzureADGroup -SearchString &quot;$S&quot;<br\/><br\/>$ZListMembers = @()<br\/>$ZListMembers = Get-AzureADGroupMember -ObjectId $List.ObjectId | Where-Object {$_.UserPrincipalName -like &#039;*$ZSourceDomain&#039;}<br\/><br\/>$ZListMembersUPN = @()<br\/>$ZListMembersUPN = $ZListMembers.UserPrincipalName<br\/><br\/>$CListMembers = @()<br\/>$CListMembers = Get-AzureADGroupMember -ObjectId $List.ObjectId | Where-Object {$_.UserPrincipalName -like &#039;*$CSourceDomain&#039;}<br\/><br\/>$CListMembersUPN = @()<br\/>$CListMembersUPN = $CListMembers.UserPrincipalName<br\/><br\/>#connect to source tenant , have to save creds if we are working with a lot of groups if not comment out $fpassword and $fpassword\u00a0<br\/>#connect to destination tenant Azure AD<br\/>#https:\/\/sid-500.com\/2020\/12\/01\/powershell-connect-to-azure-with-stored-credentials\/<br\/>$fpassword = ConvertTo-SecureString (Unprotect-CmsMessage -Path C:\\Scripts\\fpwd.txt) -AsPlainText -Force<br\/>$fcred= New-Object System.Management.Automation.PSCredential (&#039;XXXXXXX&#039;, $fpassword)<br\/>Connect-AzureAD -credential $fcred<br\/><br\/>New-AzureADGroup -DisplayName $List.DisplayName -MailEnabled $false -SecurityEnabled $true -MailNickName &quot;NotSet&quot;<br\/><br\/>foreach($ZListMemberUPN in $ZListMembersUPN){<br\/><br\/><br\/>add-azureadgroupmember -Identity $List.DisplayName -Member $ZListMemberUPN.replace(&quot;$ZSourceDomain&quot;,&quot;$DestinationDomain&quot;)<br\/><br\/>}<br\/><br\/>foreach($CListMemberUPN in $CListMembersUPN){<br\/><br\/><br\/>add-azureadgroupmember -Identity $List.DisplayName -Member $CListMemberUPN.replace(&quot;$CSourceDomain&quot;,&quot;$DestinationDomain&quot;)<br\/><br\/>}<br\/><br\/><br\/><br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <a href=\"https:\/\/github.com\/pariswells\/pariswells.com\/blob\/master\/copy-adzuread-SecurityGroups%2Bmembers.ps1\" title=\"See copy-adzuread-SecurityGroups%2Bmembers.ps1\" target=\"_blank\" class=\"code-embed-name\">copy-adzuread-SecurityGroups%2Bmembers.ps1<\/a> <a href=\"https:\/\/raw.github.com\/pariswells\/pariswells.com\/master\/copy-adzuread-SecurityGroups%2Bmembers.ps1\" title=\"Back to copy-adzuread-SecurityGroups%2Bmembers.ps1\" class=\"code-embed-raw\" target=\"_blank\">view raw<\/a> <\/div> <\/div><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6124","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=6124"}],"version-history":[{"count":4,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6124\/revisions"}],"predecessor-version":[{"id":6365,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6124\/revisions\/6365"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=6124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=6124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=6124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}