Powershell – List and Assign Office 365 Licenses

Microsoft are adding more and more functionality for administrators in Office 365 – Unfortunately, there is still no ‘easy’ and quick way to assign a new license, in bulk, to a lot of existing users. Specifically, the OFFICESUBSCRIPTION_STUDENT that was introduced to provide Office 365 licenses to Students was the one I was looking for.

Below are some quick Powershell commands to get you started (assuming that you know how to connect to Office 365 Exchange Online via Powershell)

List SKUs / Licenses Available
This will list your organisation before the SKU name, e.g. YOUR_ORG:OFFICESUBSCRIPTION_STUDENT


Add Single License to User
You can pipe this | with Get-MsolUser to set the license on a single user.


Bulk Add by Domain Name
If you have multiple domains, or sub-domains you can assign licenses in bulk easily. Replace the ‘your org’ strings below to match your setup though.

Get-MsolUser -DomainName staff.yourorg.com -All | Set-MsolUserLicense -AddLicenses YOUR_ORG:OFFICESUBSCRIPTION_STUDENT

You can use multiple filters on the last command and just pipe the second command using the |. This could be by name, by group or domain as show above.

Powershell – Update Active Directory Users Profile Data

Here is a small snippet to bulk update an OU with user profile data. Recently used to fill in default values for AD.

Change the OU and Domain details to suit; Requires full LDAP path (e.g. YOURDOMAIN.LOCAL would be DC=YOURDOM,DC=LOCAL)

Import-Module ActiveDirectory 
$Users = Get-ADUser -Filter * -SearchBase "OU=Users,DC=YOURDOM,DC=LOCAL"

foreach($User in $Users) {
    # Update properties.
    $User.postalAddress = "Address Line 1"
	$User.street = "Address Line 2"
	$User.telephoneNumber = "00000 00000000"
	$User.department = "Your Department"
	$User.wWWHomePage = "www.website.com"
	$User.postOfficeBox = "England"
	$User.city = "City"
	$User.state = "County"
	$User.postalCode = "Post Code"
    Set-ADUser -Instance $User

Always be careful bulk updating users via script. More profile values can be found in the Advanced Attribute Editor in the Active Directory Snap-In.

Powershell – Export All Email Addresses from Office 365 Exchange Online

See below to export all E-Mail addresses from Office 365 Exchange Online. If you have never connected to Powershell before then you need to download the Office 365 Powershell Cmdlets from Microsoft.

Connecting to Powershell is easy: Installing Windows PowerShell cmdlets only sets the stage.

This does nothing for actually connecting them to the Microsoft Office 365 cloud. Connecting to the cloud requires a cumbersome—but straightforward—five-step process:

1. Import your newly installed Online Services module
2. Enter Office 365 credentials
3. Create a remote session
4. Import that session’s commands into your local Windows PowerShell session
5. Connect to Office 365 services After installing the Cmdlets, open up Powershell and type the following:

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection

This will prompt for a login username and password; use your administrator login for Office 365. You next need to import the session:

Import-PSSession $session

This should connect you to Powershell. If you cannot execute commands you need to set the execution policy to Remote-Signed, as below:

Set-ExecutionPolicy RemoteSigned

After this is done you’ll be able to run your command to export data. Running the following snippet will export ALL email addresses in your organization to a CSV file called addresses.csv.

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv addresses.csv

Have fun.