{"id":17691,"date":"2010-07-21T00:01:00","date_gmt":"2010-07-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/07\/21\/hey-scripting-guy-how-can-i-copy-group-memberships-from-one-computer-account-to-another-in-active-directory-domain-services\/"},"modified":"2010-07-21T00:01:00","modified_gmt":"2010-07-21T00:01:00","slug":"hey-scripting-guy-how-can-i-copy-group-memberships-from-one-computer-account-to-another-in-active-directory-domain-services","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-copy-group-memberships-from-one-computer-account-to-another-in-active-directory-domain-services\/","title":{"rendered":"Hey, Scripting Guy! How Can I Copy Group Memberships from One Computer Account to Another in Active Directory Domain Services?"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/p>\n<p>Hey, Scripting Guy! I want to be able to copy the group memberships from one computer account in Active Directory Domain Services (AD DS) to another computer account in AD DS by using Windows PowerShell 2.0 and Active Directory cmdlets. It would be great if the script would also prompt me before copying each group to the targeted computer object. In this way, I could simply spin up a new computer account, clone group memberships, and be done with the operation. It would be way faster than manually typing in all the group memberships. <\/p>\n<p>As it is now, I have to find a computer account, click to see the group memberships, flip over to the new computer, and then switch back to the original computer&mdash;and do this several dozen times until I have completed provisioning the new computer object. After I am done, I have to go through the entire operation a second time to ensure I got it right. If I get disturbed with a phone call, I have to start over again. Needless to say, it sometimes takes nearly an entire hour to complete the process, and I still sometimes mess it up. Help! We have reduced staff, and I am afraid to complain about not having enough time to complete my work. I will be your friend for life if you can show me how to do this quicker. <\/p>\n<p>&#8212; KB<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/p>\n<p>Hello KB, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Today is going to be an awesome day! I helped a network administrator in Florida solve a problem with his Outlook automation script that quit working when they migrated from Outlook 2003 to Outlook 2010 (it was a change to the object model). The script, unfortunately, was one that was used to tally absentee reports from an extremely large school, and so he was having to do things manually until the script was repaired. Craig and I have a meeting with our managers to wrap up the results from the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/04\/05\/2010-scripting-games-all-links-on-one-page.aspx\">2010 Scripting Games<\/a> (the 2010 Scripting Games were extremely successful so the meeting should be pleasant), and now I am about to make a new friend for life! WOOHOO!!! What an awesome way to start out the day. (Would you consider naming your first child after me? Think it over.)<\/p>\n<p>KB, your idea sounds as if it would be a useful thing to do, so I decided to write a script called Copy-GroupMembersForComputer.ps1. The Copy-GroupMembersForComputer.ps1 script relies on the <strong>ActiveDirectory<\/strong> module, and therefore uses the <strong>Get-MyModule<\/strong> function to ensure the module is available. It then uses the <strong>Get-GroupsToMigrate<\/strong> function to copy groups from the reference computer. (This function will not be discussed in today&rsquo;s article.) Each group is presented, which allows you to choose to copy the group or not. After you have cycled through the groups from the reference computer, the groups are added to the target computer. The complete Copy-GroupMembersForComputer.ps1 script is shown here. <\/p>\n<p><strong>Copy-GroupMembersForComputer.ps1<\/strong><\/p>\n<p><strong><\/strong><\/p>\n<div class=\"code\"><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #2b91af\">$identity<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;WinXP&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #2b91af\">$target<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Win7-pc&#8221;      <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-MyModule<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Param([string]<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #000000\">(Get-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(Get-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ListAvailable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Where-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">})<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Import-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">available<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">then<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">import<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">available<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">already<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">loaded<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">get-MyModule<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GroupsToMigrate<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #2b91af\">$identity<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">@()<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #2b91af\">$groups<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-AdComputer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-property<\/span><span style=\"color: #808080\"> &#8220;MemberOf&#8221;&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$group<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$groups<\/span><span style=\"color: #000000\">.memberOf)<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #2b91af\">$reply<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Read-Host<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Prompt<\/span><span style=\"color: #808080\"> &#8220;add group $($group) `r`ny \/ n&#8221;      <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$reply<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #808080\"> &#8220;y&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$group<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Return<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GroupsToMigrate<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ENTRY<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">POINT<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">TO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SCRIPT<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(Get-MyModule<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\"> &#8220;ActiveDirectory&#8221;<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GroupsToMigrate<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$identity<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Get-ADComputer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$target<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add-ADPrincipalGroupMembership<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-MemberOf<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$array<\/span> <\/div>\n<p>To view and modify a computer&rsquo;s group membership, you can use the Active Directory Users and Computers MMC. Right-clicking a computer and selecting the <strong>Member Of<\/strong> tab displays the computer&rsquo;s group membership information, as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3683.hsg072110011_079F118F.jpg\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"Image of computer's group membership information\" border=\"0\" alt=\"Image of computer's group membership information\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5430.hsg072110011_thumb_51BA1C74.jpg\" width=\"604\" height=\"677\" \/><\/a> <\/p>\n<p>Once the ActiveDirectory module has been loaded, the Get-GroupsToMigrate function is called. The reference computer is passed via the &ndash;identity parameter. An array will be used to hold the groups that are to be migrated to the target computer. The Get-ADComputer cmdlet is used to retrieve the groups stored in the MemberOf Active Directory attribute. Because the MemberOf property is not automatically retrieved, it must be specifically requested via the &ndash;property parameter. This is the same behavior as the Get-ADUser cmdlet that was looked at <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/07\/20\/hey-scripting-guy-how-can-i-use-windows-powershell-to-identify-inactive-user-accounts-in-active-directory-domain-services.aspx\">yesterday<\/a>. <\/p>\n<p>The groups that the computer object is a member of are stored in the <strong>$groups<\/strong> variable. This section of the <strong>Get-GroupsToMigrate<\/strong> function is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GroupsToMigrate<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #2b91af\">$identity<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">@()<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #2b91af\">$groups<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-AdComputer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-property<\/span><span style=\"color: #808080\"> &#8220;MemberOf&#8221;      <br \/><\/span><\/div>\n<p>To display each of the groups, the <strong>Foreach<\/strong> statement is used to walk through the collection of groups. The <strong>Read-Host<\/strong> cmdlet is used to prompt the user to keep or discard the group. The reply to the prompt is stored in the <strong>$reply<\/strong> variable. If the reply contains the letter &ldquo;y,&rdquo; the group is added to the array stored in the <strong>$array<\/strong> variable. After all of the groups have been processed, the array containing selected group names is returned to the calling code. This section of the script is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #0000ff\">Foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$group<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$groups<\/span><span style=\"color: #000000\">.memberOf)<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #2b91af\">$reply<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Read-Host<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Prompt<\/span><span style=\"color: #808080\"> &#8220;add group $($group) `r`ny \/ n&#8221;      <\/p>\n<p><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$reply<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #808080\"> &#8220;y&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$group<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #000000\">Return<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GroupsToMigrate<\/span> <\/div>\n<p>&nbsp;<\/p>\n<p>When the script is run inside the Windows PowerShell ISE, the <strong>Read-Host<\/strong> cmdlet produces a popup dialog box, which is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4265.hsg072110021_3F0522BD.jpg\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"Image of dialog box produced by Read-Host cmdlet\" border=\"0\" alt=\"Image of dialog box produced by Read-Host cmdlet\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7485.hsg072110021_thumb_6C864280.jpg\" width=\"604\" height=\"194\" \/><\/a> <\/p>\n<p>If the script is run inside the Windows PowerShell console, the <strong>Read-Host<\/strong> cmdlet will pause the script execution, as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8562.hsg072110031_370D805B.jpg\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"Image of paused script execution\" border=\"0\" alt=\"Image of paused script execution\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0068.hsg072110031_thumb_36A14D66.jpg\" width=\"604\" height=\"199\" \/><\/a> <\/p>\n<p>The calling code stores the returned array of groups in the <strong>$array<\/strong> variable:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #2b91af\">$array<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GroupsToMigrate<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$identity<\/span> <\/div>\n<p>After the groups have been selected and stored in the <strong>$array<\/strong> variable, the <strong>Get-ADComputer<\/strong> cmdlet is used to retrieve the target computer. The resulting computer object is piped to the <strong>Add-ADPrincipalGroupMembership<\/strong> cmdlet, which is passed the array of groups. This section of the script is shown here:<\/p>\n<div class=\"code\"><span style=\"color: #808080\">     <br \/><\/span><span style=\"color: #000000\">Get-ADComputer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$target<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add-ADPrincipalGroupMembership<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-MemberOf<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$array<\/span> <\/div>\n<p>In processing the groups, I decided I did not want to select the <strong>appB<\/strong> group. The resulting groups are shown in the following image.<strong><\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6835.hsg072110041_3CE823F4.jpg\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"Image of resulting groups\" border=\"0\" alt=\"Image of resulting groups\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5736.hsg072110041_thumb_4A4E36FA.jpg\" width=\"604\" height=\"677\" \/><\/a> <\/p>\n<p>KB, that is all there is to using Active Directory cmdlets to migrate group memberships. Active Directory Week will continue tomorrow when we will talk about finding groups that are unused. <\/p>\n<p>We would love you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys, Active Directory, groups<\/strong><\/p>\n<p><strong><br \/><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I want to be able to copy the group memberships from one computer account in Active Directory Domain Services (AD DS) to another computer account in AD DS by using Windows PowerShell 2.0 and Active Directory cmdlets. It would be great if the script would also prompt me before copying each [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,3,45],"class_list":["post-17691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I want to be able to copy the group memberships from one computer account in Active Directory Domain Services (AD DS) to another computer account in AD DS by using Windows PowerShell 2.0 and Active Directory cmdlets. It would be great if the script would also prompt me before copying each [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17691"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17691\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}