Skip to content

Instantly share code, notes, and snippets.

@warfighter8
Last active October 2, 2018 12:18
Show Gist options
  • Save warfighter8/763262e08714b6be0c6fd46c9ec8d2f8 to your computer and use it in GitHub Desktop.
Save warfighter8/763262e08714b6be0c6fd46c9ec8d2f8 to your computer and use it in GitHub Desktop.
Allows creation of AD user accounts with powershell.
# This form will create an account based upon the information provided in the fields.
# After clicking "Create" it will prompt for a password in the original powershell window (should still be open in the background.
# Once the account is created it will be set to "Smart Card Required" using the upn field followed by "@company"
# This can be changed by changing "SmartCardLogonRequired" to $false. Note if you leave it enabled and then uncheck it manually you will have to reset their password
# Set up the form ========================================================= ==
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
import-module ActiveDirectory
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Create Account"
$objForm.Size = New-Object System.Drawing.Size(600,400)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
#testing Data#
$FirstNameLabel = New-Object System.Windows.Forms.Label
$FIRSTNAME = New-Object System.Windows.Forms.TextBox
$UnlockAccountButton = New-Object System.Windows.Forms.Button
$InitialState = New-Object System.Windows.Forms.FormWindowState
#placing fields ===============================
$LastnameLabel = New-Object System.Windows.Forms.Label
$LastnameLabel.Location = New-Object System.Drawing.Size (20,20)
$LastnameLabel.Size = New-Object System.Drawing.Size (100,15)
$LastnameLabel.Text = "Last Name"
$objForm.Controls.Add($LastnameLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(20,40)
$objTextBox.Size = New-Object System.Drawing.Size(250,20)
$objForm.Controls.Add($objTextBox)
$FirstnameLabel = New-Object System.Windows.Forms.Label
$FirstnameLabel.Location = New-Object System.Drawing.Size (20,65)
$FirstnameLabel.Size = New-Object System.Drawing.Size (100,15)
$FirstnameLabel.Text = "First Name"
$objForm.Controls.Add($FirstnameLabel)
$objFirstNameTextBox = New-Object System.Windows.Forms.TextBox
$objFirstNameTextBox.Location = New-Object System.Drawing.Size(20,85)
$objFirstNameTextBox.Size = New-Object System.Drawing.Size(250,20)
$objForm.Controls.Add($objFirstNameTextBox)
$MiddleLabel = New-Object System.Windows.Forms.Label
$MiddleLabel.Location = New-Object System.Drawing.Size (20,110)
$MiddleLabel.Size = New-Object System.Drawing.Size (100,15)
$MiddleLabel.Text = "Middle Initial"
$objForm.Controls.Add($MiddleLabel)
$objMiddleTextBox = New-Object System.Windows.Forms.TextBox
$objMiddleTextBox.Location = New-Object System.Drawing.Size(20,130)
$objMiddleTextBox.Size = New-Object System.Drawing.Size(250,20)
$objForm.Controls.Add($objMiddleTextBox)
$LegacyLabel = New-Object System.Windows.Forms.Label
$LegacyLabel.Location = New-Object System.Drawing.Size (290,20)
$LegacyLabel.Size = New-Object System.Drawing.Size (100,15)
$LegacyLabel.Text = "Legacy Username"
$objForm.Controls.Add($LegacyLabel)
$objLegacy = New-Object System.Windows.Forms.TextBox
$objLegacy.Location = New-Object System.Drawing.Size(290,40)
$objLegacy.Size = New-Object System.Drawing.Size(250,20)
$objForm.Controls.Add($objLegacy)
$UPNLabel = New-Object System.Windows.Forms.Label
$UPNLabel.Location = New-Object System.Drawing.Size (290,65)
$UPNLabel.Size = New-Object System.Drawing.Size (100,15)
$UPNLabel.Text = "UPN"
$objForm.Controls.Add($UPNLabel)
$objUPNTextBox = New-Object System.Windows.Forms.TextBox
$objUPNTextBox.Location = New-Object System.Drawing.Size(290,85)
$objUPNTextBox.Size = New-Object System.Drawing.Size(250,20)
$objForm.Controls.Add($objUPNTextBox)
$TitleLabel = New-Object System.Windows.Forms.Label
$TitleLabel.Location = New-Object System.Drawing.Size (290,110)
$TitleLabel.Size = New-Object System.Drawing.Size (100,15)
$TitleLabel.Text = "Title"
$objForm.Controls.Add($TitleLabel)
$objTitle = New-Object System.Windows.Forms.TextBox
$objTitle.Location = New-Object System.Drawing.Size(290,130)
$objTitle.Size = New-Object System.Drawing.Size(250,20)
$objForm.Controls.Add($objTitle)
$ExpireLabel = New-Object System.Windows.Forms.Label
$ExpireLabel.Location = New-Object System.Drawing.Size (290,155)
$ExpireLabel.Size = New-Object System.Drawing.Size (100,15)
$ExpireLabel.Text = "Expiration Date"
$objForm.Controls.Add($ExpireLabel)
$objExpire = New-Object System.Windows.Forms.MonthCalendar
$objExpire.Location = New-Object System.Drawing.Size(290,175)
$objExpire.Size = New-Object System.Drawing.Size(250,20)
$objForm.Controls.Add($objExpire)
# Button and action ========================================================
$CreateAccountButton = New-Object System.Windows.Forms.Button
$CreateAccountButton.Size = New-Object System.Drawing.Size (100,30)
$CreateAccountButton.Location = New-Object System.Drawing.Size (20,320)
$CreateAccountButton.Text = "Create Account"
$objForm.Controls.Add($CreateAccountButton)
$CreateAccountButton.Add_Click(
{
$userfirst=$objFirstNameTextBox.Text
$userlast=$objTextBox.Text
$usermiddle = $objMiddleTextBox.Text
$UPN = $objUPNTextBox.Text
$Title = $objTitle.Text
$legacy = $objLegacy.Text
$displayname = $userlast +", " + $userfirst + " " + $usermiddle + " " + $Title
$principalName = $UPN + "@company"
$email = $UPN + "@xxx.zzz"
$expireDateDefault = $objExpire.SelectionStart
$expireDate = $expireDateDefault.ToShortDateString()
New-ADUser -SamAccountName $legacy -GivenName $userfirst -Surname $userlast -Initials $usermiddle -Name $displayname -DisplayName $displayname -Path 'OU=Users,OU=company,DC=domain,DC=com' -SmartcardLogonRequired $true - AccountPassword (Read-Host -AsSecureString "AccountPassword") -UserPrincipalName $principalName -Enabled $true -EmailAddress $email -AccountExpirationDate $expireDate
}
)
# Activate the form =========================================================
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment