Create
User And Assign Licenses In O365 Using PowerShell Script
Pre-Requisites:
1)
Office 365 account
(work or personal account).
2)
There are 2 binaries
that you need to install: -
·
Microsoft Online
Services Sign-In Assistant for IT Professionals RTW
·
Azure Active Directory
Module for Windows PowerShell
Both of these need to be the 64bit version to work together.
Connect to O365
1.
On your local
computer, open Windows PowerShell as an administrator (use Run as administrator).
Run this
command, and enter your Office 365 work or school account credentials.
$Credential = Get-Credential
In the Windows PowerShell Credential Request dialog box, type your Office 365 username and
password, and then click OK.
2.
To
view login credentials, run the following command –
$credential
The command returns data similar to this:
UserName
Password
--------
--------
yourname@companyname.onmicrosoft.com
System.Security.SecureString
3. Import MSOnline module to allow connectivity to Microsoft Office 365
Import-Module MsOnline
4. Establish a PS session with Microsoft Office 365
Connect-MsolService -Credential $credential
5.
Run
the following command: -
$s
= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri
https://ps.outlook.com/powershell -Credential $credential -Authentication Basic
AllowRedirection
$importresults
= Import-PSSession $s
6.
To view summary information
about your current licensing plans and the available licenses for each plan,
run the following command:
Get-MsolAccountSku
The command returns data similar to this:
AccountSkuId
ActiveUnits WarningUnits ConsumedUnits
------------
----------- ------------ -------------
companyname:ENTERPRISEPACK 0 25 6
companyname:CRMSTANDARD
0 25 1
The results contain the following information:
·
AccountSkuId
Show the
available licensing plans for your organization by using the syntax <CompanyName>:<LicensingPlan>. <CompanyName> is the value that you provided when
you enrolled in Office 365, and is unique for your organization. The <LicensingPlan> value is the same for everyone. For
example, in the value litwareinc:ENTERPRISEPACK,
the company name is litwareinc,
and the licensing plan name ENTERPRISEPACK,
which is the system name for Office 365 Enterprise E3.
·
ActiveUnits
Number of
licenses that you've purchases for a specific licensing plan.
·
WarningUnits
Number of
licenses in a licensing plan that you haven't renewed, and that will expire
after the 30-day grace period.
·
ConsumedUnits
Number of
licenses that you've assigned to users from a specific licensing plan.
7.
To
view the list of all user accounts and their licensing status, run the
following command:
Get-MsolUser
-All
Or
Get-MsolUser
In
turn, Get-MsolUser returns data similar to this:
UserPrincipalName DisplayName isLicensed
----------------- ----------- ----------
pallavias@companyname.onmicrosoft.com Pallavi AS True
zia@companyname.onmicrosoft.com Zia Siddiqui True
jyotishah@companyname.onmicrosoft.com Jyoti Shah True
sonaliwetal@companyname.onmicrosoft.com Sonali Wetal True
manjarinagle@companyname.onmicrosoft.com Manjari Nagle True
parikshitanagle123@companyname.onmicrosoft.com parikshita
nagle True
To view the list of all unlicensed user
accounts in your organization, run the following command:
Get-MsolUser
-All -UnlicensedUsersOnly
This command returns information about any
users who are not currently licensed for Office 365:
UserPrincipalName DisplayName isLicensed
----------------- ----------- ----------
BelindaN@litwareinc.com Belinda Newman False
To view the list of all licensed user accounts
in your organization, run the following command:
Get-MsolUser
-All | where {$_.isLicensed -eq $true}
The command returns data similar to this:
UserPrincipalName DisplayName isLicensed
----------------- ----------- ----------
pallavias@companyname.onmicrosoft.com Pallavi AS True
zia@companyname.onmicrosoft.com Zia Siddiqui True
jyotishah@companyname.onmicrosoft.com Jyoti Shah True
sonaliwetal@companyname.onmicrosoft.com Sonali Wetal True
manjarinagle@companyname.onmicrosoft.com Manjari Nagle True
parikshitanagle123@companyname.onmicrosoft.com parikshita
nagle True
8.
To
create an individual account, use the following syntax :
New-MsolUser
-DisplayName <DisplayName> -FirstName <FirstName> -LastName
<LastName> -UserPrincipalName <Account> -UsageLocation
<CountryCode> -LicenseAssignment <AccountSkuID> [-Password
<Password>]
This example creates an account for the United States user
named User 1, and assigns a license from the companyname:ENTERPRISEPACK (Office 365 Enterprise
E3) licensing plan :-
New-MsolUser
-DisplayName
"User 1"
-FirstName User -LastName
1 -UserPrincipalName user1@ companyname.onmicrosoft.com -UsageLocation US -LicenseAssignment companyname:ENTERPRISEPACK
In turn, this returns data similar to this:
Password UserPrincipalName DisplayName isLicensed
-------- ----------------- ----------- ----------
Cunu9586 user1@companyname.onmicrosoft.com user1
True
To
verify that the user has been assigned a license, run a command like the
following:
Get-MsolUser -UserPrincipalName
"user1@yourcompany.onmicrosoft.com"
If
everything worked as expected, you should see that Varun’s isLicensed property
is now set to True:
UserPrincipalName DisplayName
isLicensed
----------------- -----------
----------
User1@companyname.onmicrosoft.com User 1 True
Now, set password for the user account. Run the following
command: -
Set-MsolUserPassword
-UserPrincipalName user1@companyname.onmicrosoft.com -NewPassword YourPassword
-ForceChangePassword $False
or
Set-MsolUserPassword -UserPrincipalName yourname@companyname.onmicrosoft.com -NewPassword YourPassword -ForceChangePassword $True
9.
This example
shows the services that user user1@companyname.onmicrosoft.com has access to. This shows the services that are
associated with all licenses that are assigned to his account.
(Get-MsolUser -UserPrincipalName
user1@companyname.onmicrosoft.com).Licenses.ServiceStatus
In turn, this returns data similar to this:
ServicePlan ProvisioningStatus
-----------
------------------
FLOW_O365_P2
Success
POWERAPPS_O365_P2
Success
TEAMS1
Success
PROJECTWORKMANAGEMENT Success
SWAY
Success
INTUNE_O365
PendingActivation
YAMMER_ENTERPRISE
Success
RMS_S_ENTERPRISE
Success
OFFICESUBSCRIPTION
Success
MCOSTANDARD
Success
SHAREPOINTWAC
Success
SHAREPOINTENTERPRISE
Success
EXCHANGE_S_ENTERPRISE Success
Testing
Login to the O365 portal (portal.office.com)
using the credentials: -
Password: **********
Perform few operations like: -
·
Sending email
·
Go to admin portal
·
Check users
·
Check newly added
users
·
Check licenses, etc.
Script
$credential
= Get-Credential
Import-Module
MsOnline
$credential
Connect-MsolService
-Credential $credential
Get-MsolAccountSku
$s
= New-PSSession
-ConfigurationName Microsoft.Exchange
-ConnectionUri https://ps.outlook.com/powershell
-Credential $credential
-Authentication
Basic -AllowRedirection
$importresults
= Import-PSSession
$s
Get-MsolUser
(Get-MsolUser -UserPrincipalName
youname@companyname.onmicrosoft.com).Licenses.ServiceStatus
Get-MsolUser
| where {$_.isLicensed
-eq $true}
Get-MsolUser
-All -UnlicensedUsersOnly
New-MsolUser
-DisplayName "Firstname
Lastname" -FirstName Yourfirstname -LastName
Yourlastname -UserPrincipalName
youname@companyname.onmicrosoft.com
-UsageLocation
US -LicenseAssignment
companyname:O365_BUSINESS_PREMIUM
Set-MsolUserPassword
-UserPrincipalName youname@companyname.onmicrosoft.com
-NewPassword YourPassword
-ForceChangePassword $False