################################## Import ActiveDirectory ##############################################
Import-Module ActiveDirectory
################################## Import Exchange snap-in #############################################
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
########################################################################################################
clear-host
[System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 350;
$Form.Autosize = $True
$form.Text = "New AD User Creation";
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$Form.Topmost = $True
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = "Username to Copy";
##############Define text label2
$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;
$textLabel2.Autosize = $True
$textLabel2.Text = "Requested Username";
##############Define text label3
$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 95;
$textLabel3.Text = "User First Name";
##############Define text label4
$textLabel4 = New-Object “System.Windows.Forms.Label”;
$textLabel4.Left = 25;
$textLabel4.Top = 135;
$textLabel4.Text = "User Last Name";
##############Define text label5
$textLabel5 = New-Object “System.Windows.Forms.Label”;
$textLabel5.Left = 25;
$textLabel5.Top = 175;
$textLabel5.Text = "Newuser logon";
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 200;
$textBox1.Top = 10;
$textBox1.width = 200;
############Define text box2 for input
$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 200;
$textBox2.Top = 50;
$textBox2.width = 200;
############Define text box3 for input
$textBox3 = New-Object “System.Windows.Forms.TextBox”;
$textBox3.Left = 200;
$textBox3.Top = 90;
$textBox3.width = 200;
############Define text box4 for input
$textBox4 = New-Object “System.Windows.Forms.TextBox”;
$textBox4.Left = 200;
$textBox4.Top = 130;
$textBox4.width = 200;
############Define text box4 for input
$textBox5 = New-Object “System.Windows.Forms.TextBox”;
$textBox5.Left = 200;
$textBox5.Top = 170;
$textBox5.width = 200;
#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;
$textBox3.Text = $defaultValue;
$textBox4.Text = $defaultValue;
$textBox5.Text = $defaultValue;
###########Connecting Enter and Escape Keys to buttons
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.Keycode -eq "Enter") {$x=$Form.Text;$Form.Close()}})
$Form.Add_KeyDown({if ($_.Keycode -eq "Escape") {$Form.Close()}})
###########OK button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(200,220)
$OKButton.Size = New-Object System.Drawing.Size(50,35)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$Form.Close()})
###########Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(340,220)
$CancelButton.Size = New-Object System.Drawing.Size(65,35)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$Form.Close()})
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$textBox4.Text;
$textBox5.Text;
$form.Close();};
$button.Add_Click($eventHandler);
#############Add controls to all the above objects defined
$Form.Controls.Add($CancelButton);
$Form.Controls.Add($OKButton);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textLabel4);
$form.Controls.Add($textLabel5);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$form.Controls.Add($textBox3);
$form.Controls.Add($textBox4);
$form.Controls.Add($textBox5);
$ret = $form.ShowDialog();
$x = $textBox.Text
$x
#################return values
return $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text, $textbox5.Text
$return= button “User to Copy” “Requested Username” “User Firstname” “User Lastname” "Re-enter user logon name"
Below variables will get the values that had been entered by the user
$return[0]
$return[1]
$return[2]
$return[3]
$return[4]
# Gets all of the users info to be copied to the new account
#Checking the user to copy if it exist
do {
$nameds = "$textBox1.Text"
if (dsquery user -samid $nameds){"AD User Found"
}
elseif ($nameds = "null") {"AD User not Found"}
}
while ($nameds -eq "null")
#Checking if the new user exist
do {
$NewUserds = "$textBox2.Text"
While ( $NewUserds -eq "" ) { $NewUserds = "$textBox2.Text"}
$NewUser = $Newuserds
if (dsquery user -samid $NewUserds){"The AD Username entered is currently in use"}
elseif ($NewUserds = "no") {"Username is available for use"}
}
while ($Newuserds -ne "no")
# Gets all of the users info to be copied to the new account
$name = Get-AdUser -Identity $nameds -Properties *
$DN = $name.distinguishedName
$OldUser = [ADSI]"LDAP://$DN"
$Parent = $OldUser.Parent
$OU = [ADSI]$Parent
$OUDN = $OU.distinguishedName
$NewUser = "$textBox2.Text"
$firstname = "$textBox3.Text"
$Lastname = "$textBox4.Text"
$NewName = "$firstname $lastname"
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Displayname = "$FirstName.$LastName"
$Display = "$LastName, $firstname"
# Creates the user from the copied properties
New-ADUser -SamAccountName $NewUser -Name $Display -GivenName $firstname -Surname $lastname -Instance $DN -Path "$OUDN" -DisplayName $Display -AccountPassword (Read-Host "New Password" -AsSecureString) –userPrincipalName $Displayname@$domain
-Company $name.Company -Department $name.Department -Manager $name.Manager -title $name.Title -Description (Read-Host "Please Enter a Description") -Office $name.Office -City $name.city -PostalCode $name.postalcode -Country $name.country -OfficePhone
$name.OfficePhone -Fax $name.fax -State $name.State -StreetAddress $name.StreetAddress -Enabled $true
# Creates the special attributes
#Get-ADUser $NewUser -Properties mail,mailNickname, proxyAddresses, targetAddress, GivenName, Surname
Set-aduser $NewUser -Add @{proxyAddresses = "SMTP:$Displayname@hillphoenix.com", "smtp:$Displayname-hillphoenix-com@dover.mail.onmicrosoft.com"; Mail = "$Displayname@hillphoenix.com"; mailNickName = "$Displayname"; targetAddress
= "SMTP:$Displayname-HillPHOENIX-com@dover.mail.onmicrosoft.com"}
# gets groups from the Copied user and populates the new user in them
write-host "Copying Group Membership"
$groups = (GET-ADUSER –Identity $name –Properties MemberOf).MemberOf
foreach ($group in $groups) {
Add-ADGroupMember -Identity $group -Members $NewUser
}
$count = $groups.count
Write-host "New User Mail Parameters"
Get-ADUser $NewUser -Properties mail,mailNickname, proxyAddresses, targetAddress, GivenName, Surname