Copying or migrating users between groups

A common task when implementing new systems is to copy or migrate users from one AD group to another. It’s often a requirement when deploying upgraded application versions too. I wrote a small utility that makes this task easier. Being GUI based means it can be used by less technical team members as well.

The PowerShell code for this utility is below.

#region ScriptForm Designer

#region Constructor

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

#endregion

#region Post-Constructor Custom Code

#endregion

#region Form Creation
#Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
#When working with the ScriptForm designer this region and any changes within may be overwritten.
#~~< GroupMembershipMigrationToolForm >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$GroupMembershipMigrationToolForm = New-Object System.Windows.Forms.Form
$GroupMembershipMigrationToolForm.ClientSize = New-Object System.Drawing.Size(839, 479)
$GroupMembershipMigrationToolForm.Text = "Group Membership Migration Tool"
#~~< UpdateButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$UpdateButton = New-Object System.Windows.Forms.Button
$UpdateButton.Location = New-Object System.Drawing.Point(695, 422)
$UpdateButton.Size = New-Object System.Drawing.Size(75, 23)
$UpdateButton.TabIndex = 13
$UpdateButton.Text = "Update"
$UpdateButton.UseVisualStyleBackColor = $true
#~~< TextBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$TextBox2 = New-Object System.Windows.Forms.TextBox
$TextBox2.Location = New-Object System.Drawing.Point(38, 422)
$TextBox2.Size = New-Object System.Drawing.Size(635, 20)
$TextBox2.TabIndex = 12
$TextBox2.Text = ""
#~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Point(38, 395)
$Label1.Size = New-Object System.Drawing.Size(100, 23)
$Label1.TabIndex = 11
$Label1.Text = "Label1"
$Label1.add_MouseClick({UpdateButtonMouseClick($Label1)})
#~~< MigrateButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$MigrateButton = New-Object System.Windows.Forms.Button
$MigrateButton.Location = New-Object System.Drawing.Point(695, 268)
$MigrateButton.Size = New-Object System.Drawing.Size(75, 23)
$MigrateButton.TabIndex = 10
$MigrateButton.Text = "Migrate"
$MigrateButton.UseVisualStyleBackColor = $true
$MigrateButton.add_MouseClick({MigrateButtonMouseClick($MigrateButton)})
#~~< ClearButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ClearButton = New-Object System.Windows.Forms.Button
$ClearButton.Location = New-Object System.Drawing.Point(583, 268)
$ClearButton.Size = New-Object System.Drawing.Size(75, 23)
$ClearButton.TabIndex = 9
$ClearButton.Text = "Clear"
$ClearButton.UseVisualStyleBackColor = $true
$ClearButton.add_MouseClick({ClearButtonMouseClick($ClearButton)})
#~~< CloseButton1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$CloseButton1 = New-Object System.Windows.Forms.Button
$CloseButton1.Location = New-Object System.Drawing.Point(469, 268)
$CloseButton1.Size = New-Object System.Drawing.Size(75, 23)
$CloseButton1.TabIndex = 8
$CloseButton1.Text = "Close"
$CloseButton1.UseVisualStyleBackColor = $true
$CloseButton1.add_MouseClick({CloseButton1MouseClick($CloseButton1)})
#~~< ResultsLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ResultsLabel = New-Object System.Windows.Forms.Label
$ResultsLabel.Location = New-Object System.Drawing.Point(38, 119)
$ResultsLabel.Size = New-Object System.Drawing.Size(100, 15)
$ResultsLabel.TabIndex = 7
$ResultsLabel.Text = "Results"
$ResultsLabel.add_Click({Label1Click($ResultsLabel)})
#~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Point(38, 146)
$TextBox1.Multiline = $true
$TextBox1.Size = New-Object System.Drawing.Size(347, 213)
$TextBox1.TabIndex = 6
$TextBox1.Text = ""
#~~< DeleteMembersCheckBox >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$DeleteMembersCheckBox = New-Object System.Windows.Forms.CheckBox
$DeleteMembersCheckBox.Location = New-Object System.Drawing.Point(450, 146)
$DeleteMembersCheckBox.Size = New-Object System.Drawing.Size(198, 33)
$DeleteMembersCheckBox.TabIndex = 5
$DeleteMembersCheckBox.Text = "Delete members from Source Group"
$DeleteMembersCheckBox.UseVisualStyleBackColor = $true
#~~< GroupMembershipMigrationToolLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$GroupMembershipMigrationToolLabel = New-Object System.Windows.Forms.Label
$GroupMembershipMigrationToolLabel.Font = New-Object System.Drawing.Font("Tahoma", 12.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$GroupMembershipMigrationToolLabel.Location = New-Object System.Drawing.Point(258, 9)
$GroupMembershipMigrationToolLabel.Size = New-Object System.Drawing.Size(333, 23)
$GroupMembershipMigrationToolLabel.TabIndex = 4
$GroupMembershipMigrationToolLabel.Text = "Group Membership Migration Tool"
#~~< DestinationGroupLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$DestinationGroupLabel = New-Object System.Windows.Forms.Label
$DestinationGroupLabel.Location = New-Object System.Drawing.Point(450, 54)
$DestinationGroupLabel.Size = New-Object System.Drawing.Size(141, 23)
$DestinationGroupLabel.TabIndex = 3
$DestinationGroupLabel.Text = "Destination Group"
$DestinationGroupLabel.add_Click({Label2Click($DestinationGroupLabel)})
#~~< SourceGroupLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$SourceGroupLabel = New-Object System.Windows.Forms.Label
$SourceGroupLabel.Location = New-Object System.Drawing.Point(38, 54)
$SourceGroupLabel.Size = New-Object System.Drawing.Size(141, 23)
$SourceGroupLabel.TabIndex = 2
$SourceGroupLabel.Text = "Source Group"
#~~< ListBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ListBox2 = New-Object System.Windows.Forms.ListBox
$ListBox2.FormattingEnabled = $true
$ListBox2.Location = New-Object System.Drawing.Point(450, 80)
$ListBox2.SelectedIndex = -1
$ListBox2.Size = New-Object System.Drawing.Size(347, 17)
$ListBox2.TabIndex = 1
#~~< ListBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ListBox1 = New-Object System.Windows.Forms.ListBox
$ListBox1.FormattingEnabled = $true
$ListBox1.Location = New-Object System.Drawing.Point(38, 80)
$ListBox1.SelectedIndex = -1
$ListBox1.Size = New-Object System.Drawing.Size(347, 17)
$ListBox1.TabIndex = 0
$GroupMembershipMigrationToolForm.Controls.Add($UpdateButton)
$GroupMembershipMigrationToolForm.Controls.Add($TextBox2)
$GroupMembershipMigrationToolForm.Controls.Add($Label1)
$GroupMembershipMigrationToolForm.Controls.Add($MigrateButton)
$GroupMembershipMigrationToolForm.Controls.Add($ClearButton)
$GroupMembershipMigrationToolForm.Controls.Add($CloseButton1)
$GroupMembershipMigrationToolForm.Controls.Add($ResultsLabel)
$GroupMembershipMigrationToolForm.Controls.Add($TextBox1)
$GroupMembershipMigrationToolForm.Controls.Add($DeleteMembersCheckBox)
$GroupMembershipMigrationToolForm.Controls.Add($GroupMembershipMigrationToolLabel)
$GroupMembershipMigrationToolForm.Controls.Add($DestinationGroupLabel)
$GroupMembershipMigrationToolForm.Controls.Add($SourceGroupLabel)
$GroupMembershipMigrationToolForm.Controls.Add($ListBox2)
$GroupMembershipMigrationToolForm.Controls.Add($ListBox1)

#endregion

#region Custom Code

#endregion

#region Event Loop

function Main{
     [System.Windows.Forms.Application]::EnableVisualStyles()
     [System.Windows.Forms.Application]::Run($GroupMembershipMigrationToolForm)
}

#endregion

#endregion

#region Event Handlers



function MigrateButtonMouseClick( $object ){

}

function ClearButtonMouseClick( $object ){

}

function CloseButton1MouseClick( $object ){

        

}


function UpdateButtonMouseClick( $object ){

    

    # **** Change the -searchbase entry below to point at the Active Directory OU containing Application Deployment Groups ****
    $Groups = get-adgroup -filter * -searchbase "OU="Application Deployment Groups", OU=Groups, OU=EUC, OU="Thames Water", DC=TWUTIL, DC=NET" | sort Name
    If ($SearchBase -ne "")
    {
    $Groups = get-adgroup -filter * -searchbase $SearchBase | sort Name
        if ($error[0] -gt "")
            {
                #$TextBox2.AppendText("Unable to get AD group list" + "`r`n")
                #$TextBox2.AppendText("Error message " + $Error[0] + "`r`n")
                $error.Clear()
            }
        else
            {
                #$TextBox2.AppendText("Successfully added " + $CompName + " to " + $SelectedGroup + "`r`n") 
                ForEach ($Group in $Groups)
                {
                    ListBox1.Items.Add($Group.Name)
                }
            }


Set-ItemProperty -path HKCU:\Software\GroupMembershipMigrationTool -Name SearchBase -Value $TextBox1.text
    $SearchBase= $TextBox1.Text 
    Try {$Groups = get-adgroup -filter * -searchbase $SearchBase | sort Name}
        Catch
            {
                #$TextBox2.AppendText("Unable to get AD group list" + "`r`n")
                #$TextBox2.AppendText("Error message " + $Error[0] + "`r`n")
                $error.Clear()
            }

        }
}



function Label1Click( $object ){

}

  


Main # This call must remain below all other event functions

#endregion