Powershell Script for Creating a New MSOL User

Use the below script in Windows Powershell to automate the creation of users in MSOL.
Nothing complicated, just straight forward automation…


# Creating a new MSOL User

# Get administrative user credentials
$msolcred = get-credential

#Establish Remote PowerShell Session to Exchange Online
connect-msolservice -credential $msolcred

#Get the list of available licences

#Prompt to select the right licence
$title = “Creating an MSOL User”
$message = “In the window behind this prompt are the licences available to add to the account.

If the ‘ActiveUnits’ column is equal to the ‘ConsumedUnits’ then a licence must be added via the web portal.
If the ‘ActiveUnits’ column is more than the ‘ConsumedUnits’ then there is a spare licence and you can continue.

Once you know there is a spare licence, Click OK below

In the next prompt, type the Licence you wish to add as it is displayed

$complete = New-Object System.Management.Automation.Host.ChoiceDescription “&OK”, `
“Moving on…”

$options = [System.Management.Automation.Host.ChoiceDescription[]]($complete)

$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
0 {“OK selected.”}

#Gather info to create the user
$Lic = Read-Host “Enter a Licence above to add to the account (I.e. business365:EXCHANGESTANDARD)”
$UPN = Read-Host “Enter the users Email Address (I.e. your.name@mydomain.com)”
$DisplayName = Read-Host “Enter the Display Name (I.e. Surname, Firstname)”
$FirstName = Read-Host “Enter the users First Name”
$Surname = Read-Host “Enter the users Surname”
$Pass = Read-Host “Enter the password for the new account”

#Create the user
New-MsolUser –UserPrincipalName $UPN –DisplayName $DisplayName -FirstName $FirstName –LastName $Surname –Password $Pass -UsageLocation GB -LicenseAssignment $Lic

#Set password and set to never expire
“Setting the password to never expire…”
Set-MsolUserPassword –UserPrincipalName $UPN –NewPassword $Pass -ForceChangePassword $false
Get-MsolUser|Set-MsolUser -PasswordNeverExpires $True

#Prompt about setting the password to never expire
$title = “Creating an MSOL User”
$message = “This prompt is allowing time for setting the password to never expire.

I will now do this for you…But better check after the account is created as I am a little rubbish at applying the setting, even though my code is correct……

Moving on…

$complete = New-Object System.Management.Automation.Host.ChoiceDescription “&OK”, `
“Moving on…”

$options = [System.Management.Automation.Host.ChoiceDescription[]]($complete)

$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
0 {“Done”}

#Switching from MSOL
“Switching to Office 365 from MSOL to set the regional mailbox configuration using ‘Set-MailboxRegionalConfiguration'”

#Connect the session to O365 to add the Regional Config
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365cred -Authentication Basic -AllowRedirection
Import-PSSession $session

#Set the Regional Language settings
“Running Cmdlet ‘Set-MailboxRegionalConfiguration'”
Set-MailboxRegionalConfiguration -identity $UPN -Language en-GB -DateFormat dd/MM/yyyy -TimeFormat HH:mm -TimeZone “GMT Standard Time”

#End the session
“The user has been created”
“Removing the session…”
Remove-PSSession $Session


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s