Skip to content

Instantly share code, notes, and snippets.

@kbhunut
Last active March 31, 2025 12:35
Show Gist options
  • Save kbhunut/fe188bf024c23d06c67f8ddbe7fbabcc to your computer and use it in GitHub Desktop.
Save kbhunut/fe188bf024c23d06c67f8ddbe7fbabcc to your computer and use it in GitHub Desktop.
Office 365 Onboarding
#Requires -Version 7.0
# If you powershell version is lower than 7. Must update to the latest version.
# Install Azure Graph Powershell Module
# Run this Command: Install-Module -Name AzureAD
# https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-office-365-powershell#connect-with-the-azure-active-directory-powershell-for-graph-module
# https://docs.microsoft.com/en-us/office365/enterprise/powershell/manage-user-accounts-and-licenses-with-office-365-powershell
$Credential = Get-Credential
Connect-AzureAD -Credential $Credential
# Tried of input credential every time? You can export credential and import it.
# Export Credential to XML (One time)
Get-Credential | Export-CliXml -Path "$($env:userprofile)\cred.xml"
# Import it
$Credential = Import-CliXml "$($env:userprofile)\cred.xml"
Connect-AzureAD -Credential $Credential
# We need to connect to Exchange Online via Powershell also to use the command to turn off OWA
# https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps#connect-to-exchange-online
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
# This will import exchange CMDlets and execute it remotely.
Import-PSSession $Session -DisableNameChecking
# Save this user to a variable
# https://docs.microsoft.com/en-us/office365/enterprise/powershell/view-user-accounts-with-office-365-powershell
$user = Get-AzureADUser -Filter "userPrincipalName eq '[email protected]'"
# Display user
$user
# https://docs.microsoft.com/en-us/office365/enterprise/powershell/assign-licenses-to-user-accounts-with-office-365-powershell
# First we need to identify the available SKU in Azure. This should give info such as E3 and etc....
Get-AzureADSubscribedSku | Select SkuPartNumber
# Next we need to set user location to US or other country 2 letter country code.
$user | Set-AzureADUser -UsageLocation "US"
# Assign License
$planName="<license plan name from the list of license plans from the first command>"
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $planName -EQ).SkuID
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.AddLicenses = $License
$user | Set-AzureADUserLicense -AssignedLicenses $LicensesToAssign
# Turn Off OWA
# https://docs.microsoft.com/en-us/powershell/module/exchange/client-access/get-casmailbox?view=exchange-ps#description
# https://docs.microsoft.com/en-us/powershell/module/exchange/client-access/set-casmailbox?view=exchange-ps
Get-CASMailbox "[email protected]" | Set-CASMailbox -OWAEnabled $false
# Bulk Assignment
# CSV Header: UserPrincipalName, SkuPartNumber, Location
# Connect to Azure and Exchange online first
Import-CSV o365_assignments.csv | Foreach {
$user = Get-AzureADUser -Filter "userPrincipalName eq $($_.UserPrincipalName)"
$user | Set-AzureADUser -UsageLocation $_.Location
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $_.SkuPartNumber -EQ).SkuID
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.AddLicenses = $License
$user | Set-AzureADUserLicense -AssignedLicenses $LicensesToAssign
Get-CASMailbox $_.UserPrincipalName | Set-CASMailbox -OWAEnabled $false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment