Monday, 18 December 2017

Create New Office 365 User Account using Powershell

Office 365 is not a single service and it is the brand name Microsoft uses for a group of cloud services along with new features in desktop Office suite. Azure Active Directory is the storage service for Office 365 user identities, we can easily create a new user account using the Azure AD powershell cmdlet New-MsolUser.

Note: Before proceed, Install and Configure Azure AD PowerShell and run the below command to connect Azure AD PowerShell module.
Import-Module MSOnline
$msolCred = Get-Credential
Connect-MsolService –Credential $msolCred

Summary:

Create new Office 365 user account :

Use the below command to create an individual user account without license subscription:
New-MsolUser -DisplayName "Steve Smith" -UserPrincipalName steves@contoso.com -Password "pass@word1" -FirstName Steve -LastName Smith
Note: The input -Password is the optional parameter if you don't specify a password, a random password will be assigned to the user account, and the password is visible in the results of the command.

Create new user account and assign license :

You don't need to assign a license when you create user account, but without license the new user can't access any of Office 365 services. Before proceed you need to get AccountSkuId of the license plan that you want to assign to new user, you can use the Get-MsolAccountSku cmdlet to get the available licensing plans ( AccountSkuId ) for your organization. For more info, see Get list of Office 365 licenses using Powershell.

The below command creates new Office 365 user account named "Steve Smith", set the Usage Location as United States and assign the license plan "contoso:ENTERPRISEPREMIUM".
New-MsolUser -DisplayName "Steve Smith" -UserPrincipalName steves@contoso.com -UsageLocation US -LicenseAssignment "contoso:ENTERPRISEPREMIUM" -FirstName Steve -LastName Smith
Note: The Usage Location is mandatory for the user when you assign license.

Create Bulk Office 365 user accounts from CSV file

Creating bulk user accounts is one of the important task for every Office 365 admin when multiple new employees join to organisation. In this case we can import user account details from CSV (Comma-Separated Value) file. Consider the CSV file NewOffice365Users.csv which contains user information with the column headers UserPrincipalName, FirstName, LastName, DisplayName, UsageLocation, AccountSkuId.

The below command creates user accounts by importing user details from the file "C:\NewOffice365Users.csv", and exports the results to "C:\NewAccountResults.csv".
Import-Csv "C:\NewOffice365Users.csv" | ForEach-Object {
New-MsolUser -DisplayName $_.DisplayName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId -FirstName $_.FirstName -LastName $_.LastName 
} | Export-CSV "C:\\NewAccountResults.csv" -NoTypeInformation -Encoding UTF8
Note: We didn't given the input parameter -Password in above command, so a random password will be assigned to every user accounts, and new password will be available in the exported csv file "C:\NewAccountResults.csv".

Advertisements
Advertisements

No comments:

Post a Comment