Now that we know how to set up our PowerShell environment for Office 365, we will look at the available commands.
It is impossible to cover all the commands in a series of article, this is why, I will concentrate on the most important ones, the commands that you need the most.
Once you are comfortable with these commands, you can find a lot of resources and help online, you can simply check the Microsoft Docs.
Most of the commands have a Verb (Get, Set) and a Noun.
Most of the PowerShell Office365 commands look like All the commands look like <Verb>-Msol<Noun>
1- List all the users in Office365 Tenant
As we’ve seen in the previous article, you can list all the users in you tenant by simply running the following command.
If I wanted to filter my users by department, I could have done:
Get-MsolUser -Department IT
We can filter by any property of the user object (e.g. -City Algiers).
1.1 Export the users list
If you want to export the users list to a .txt file or a .csv, you can simply do:
Get-MsolUser > users.txt
2- Update a user details
You can simply change any of the properties of a user object, this can be achieved by simply running the following command:
Set-MsolUser -UserPrincipalName email@example.com -LastName "Daoudi" -DisplayName "Samir D"
In this example, I have changed my own Last name and display name in my Dev Office 365 Tenant.
PS: In the Office 365 Admin center might take couple of seconds to show the update accounts.
- Get the Office 365 Licences
If we want to list the different licences that we have in our office 365 tenant including the active, consumed and warning Units, we can simply run the following command:
If we wanted to see the status of these licences, and display whether they are all activated and used, we can simply do :
$sku = Get-MsolAccountSku $sku.ServiceStatus
3- Create a new user in Office365
In order to create a new user in your Office365 tenant, you can simply use the New-MsolUser command, which take couple of
New-MsolUser -UserPrincipalName firstname.lastname@example.org -DisplayName "Net RM" -FirstName "Net" -LastName "RM" -LicenseAssignment logisam:FLOW_FREE -UsageLocation CA
We need to define some mandatory properties, such as the UserPrincipleName, FirstName, LastName , but most importantly the LicenceAssignment and the Usage Location.
PS: You can’t create a user without the UsageLocation property.
4- Limiting Licence Usage with PowerShell
We can create a custom licence pack, which will contain only the products we want to assign to the user.
$NewSku= New-MsolLicenseOptions -AccountSkuId logisam:O365_BUSINESS_PREMIUM -DisabledPlans EXCHANGE_S_FOUNDATION, VISIOONLINE
Now, to set this newly created custom licence, we can simply do:
Set-MsolUserLicense -UserPrincipalName "email@example.com" -LicenseOptions $NewSku