Creating new user with PowerShell · 2009-12-05 19:35 by Black in Scripts
Exchange Server 2007 has removed the ActiveDirectory integration of previous versions, creating a user in AD does no longer also create and link a Mailbox. To create everything properly, the Management Console for Exchange or PowerShell has to be used.
This PowerShell Script creates a new User with parameters set in a GUI. .Net is used to display a Dialog Box, the text boxes are then evaluated and used to create a new user. After that, that user is changed to reflect the remaining settings.
Additional features include the creation of file shares on the server, automatic generation of the E-Mail address with some limited CharSet cleaning, live updating UI, user expiration date setting and more. The whole script is quite customized to the environment it is used in, but I am sure the core can be used by anyone.
An interesting concept this PowerShell Script show are the creation and event based updating of .Net Widgets. updateUI is a function that is called as event handler for the text box, it can execute input validation, update other parts of the UI or do anything else. (See the linked source for more context):
new-user.ps1 [20.72 kB]
- $form = new-object System.Windows.Forms.form
- $form.Text = "Exchange 2007 User Create Form"
- $form.size = new-object System.Drawing.Size(440,550)
- $form.AutoSize = $true
- $form.AutoSizeMode = "GrowOnly"
- ### FirstName
- $posY += $lineHeight
- # Add FirstName Box
- $firstNameTextBox = new-object System.Windows.Forms.TextBox
- $firstNameTextBox.Location = new-object System.Drawing.Size($posXControl,$posY)
- $firstNameTextBox.size = new-object System.Drawing.Size($controlWidth,$controlHeight)
- $firstNameTextBox.add_TextChanged({updateUID})
- $form.Controls.Add($firstNameTextBox)
Creating a new User with PowerShell is easy thanks to the new-mailbox cmd-let the Exchange Integration installs. But setting some of the properties was rather complicated. For some, an AD Object has to be generated:
new-user.ps1 [20.72 kB]
- # General Stuff (alternative: use set-user, for some of those)
- $user = get-user -identity $upn
- $aduser = [ADSI]("LDAP://"+$user.DistinguishedName)
- if ($desc -ne "")
- {
- $aduser.description = $jobDescDrop.Text
- }
- if ($phone -ne "")
- {
- $aduser.telephonenumber = $phone
- }
- if ($webpage -ne "")
- {
- $aduser.wwwhomepage = $webpage
- }
- $aduser.company = $company
- $aduser.department = $department
- # FS
- $aduser.profilePath = $pathPro + $alias + "\%osversion%"
- $aduser.homeDrive = "P:"
- $aduser.homeDirectory = $pathBase + $alias + "$"
- # Commit Settings
- $aduser.SetInfo()
Others such as setting the expiration date of an account to “never expires” require to use a more arcane syntax:
new-user.ps1 [20.72 kB]
- # Hard to change Expiration Date is set directly
- #$aduser.psbase.InvokeGet("AccountExpirationDate")
- $aduser.psbase.InvokeSet("AccountExpirationDate", $validUntil)
- $aduser.psbase.CommitChanges()