Create Office 365 accounts using Powershell

When Office 365 was released, provisioning accounts with DirSync was the only available method. There was no support for Powershell which was unfortunate. Since then Microsoft has released Powershell support and readily documentation. This article describes a quick way to create an account in Office 365 using Powershell.

To begin using the Office 365 cmdlets, you first need to install them. This is described under section Install the Office 365 cmdlets. Use x64 and make sure you set ExecutionPolicy to RemoteSigned.

Set-ExecutionPolicy RemoteSigned

Then you need to import Powershell module for Office 365.

Import-Module MSOnline

Create a session.

$Username = "admin@mydomain.onmicrosoft.com"
$Password = ConvertTo-SecureString "P@ssword" -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password
Connect-MsolService -Credential $Credentials

Create a new User.

New-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -DisplayName "John Doe" -FirstName "John" -LastName "Doe"

New-MsolUser will create a random password and return a MsolUser object which contains the new password. However, when logging in to the portal, user will face an error message.

This is beacuse a user needs to be assigned to a subscription. To do this UsageLocation nedds to be set and second a license added:

Set-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -UsageLocation "US"
Set-MsolUserLicense -UserPrincipalName johndoe@mydomain.onmicrosoft.com -AddLicenses "tengil:ENTERPRISEPACK"

It is possible (and recommended) to create a user with a license in a single call.

New-MsolUser -UserPrincipalName johndoe@mydomain.onmicrosoft.com -DisplayName "John Doe" -FirstName "John" -LastName "Doe" -UsageLocation "US" -LicenseAssignment "Contoso:BPOS_Standard"

A simple script to summarize this:


param(
[parameter(Mandatory = $true)][string]$AdminUsername,
[parameter(Mandatory = $true)][string]$AdminPassword,
[parameter(Mandatory = $true)][string]$UserPrincipalName,
[parameter(Mandatory = $true)][string]$DisplayName
[parameter(Mandatory = $true)][string]$FirstName,
[parameter(Mandatory = $true)][string]$LastName,
[parameter(Mandatory = $true)][string]$UsageLocation,
[parameter(Mandatory = $true)][string]$LicenseAssignment
)

function New-O365User()
{
if((Get-Module MSOnline) -eq $null)
{
Import-Module MSOnline
}

$pwd = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential $AdminUsername,$pwd
Connect-MsolService -Credential $Credentials

New-MsolUser -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -FirstName $FirstName -LastName $LastName -UsageLocation $UsageLocation -LicenseAssignment $LicenseAssignment

}

New-O365User -AdminUsername $AdminUsername -AdminPassword $AdminPassword -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -FirstName $FirstName -LastName $LastName -UsageLocation $UsageLocation -LicenseAssignment $LicenseAssignment

Modify QuickLaunch of SharePoint web with Powershell

I had to modify the quicklauch for multiple webs. In this case Powershell is my friend. The action needed to be done was to show subwebs in QuickLaunch. If publishing feature is activated on web, this property can be set on /_layouts/AreaNavigationSettings.aspx.

AreaNavigationSettings

With Powershell the property is a bit tricky to find. First, web need to create a PublishingWeb-object.

$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

Second, the CurrentIncludeSubSites property of the Navigation property must be set to true.

$pubWeb.Navigation.CurrentIncludeSubSites = $true

The complete function looks something like this.


param(
[parameter(Mandatory = $true)][string]$Url
)

function Set-CurrentIncludeSubSites()
{
#Load assemblies
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$web = Get-SPWeb $Url

if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web) -eq $true)
{
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pubWeb.Navigation.CurrentIncludeSubSites = $true
$pubWeb.Update()
}
}

Set-CurrentIncludeSubSites -Url $Url

Save this as Set-CurrentIncludeSubSites.ps1 and run it with .\Set-CurrentIncludeSubSites.ps1 -Url http://myurl

Create an Office 365 domain

There are several ways to create a new Office 365 domain and at the moment there are several different types of domains available. There is the standard Office 365 offer, the Office 365 Enterprise Preview (built on SharePoint 2013) and finally the hard core Office 365 Developer Site.

Office 365

To create a domain for a school, company or institution, create an ordinary Office 365 domain. There is a 30 days trial and you will have time to test the domain thoroughly. If you want to use the Office 365 for Education offer you sign up for the trial, assign your institution address to it and have Microsoft verify eligibility.

http://www.microsoft.com/en-us/office365/default.aspx

Office 365 Enterprise Preview

Office 365 Enterprise Preview is the latest and coming version of Office 365, built on SharePoint 2013. You can sign up for a free trail which has a longer expiration time than the regular Office 365 trial site. With the preview you get 25 account to play around with Office, SharePoint, Lync and Exchange.

http://www.microsoft.com/office/preview/en/office-365-enterprise

Office 365 Developer Site

Office 365 Developer Site is the type of domain you need to get. With an Office 365 Developer Site you only get one account, but that’s all you need to start creating apps for Office 365.

http://msdn.microsoft.com/en-us/library/fp179924(v=office.15).aspx