April 19, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

PowerShell (Part 6) – Users and licensing

In this article, we will look at how to create users in your Office365 tenant using PowerShell.

The scenario we will be looking at is the following:

A request from the HR department to create multiple users from a list of new joiners of the company.

We will be using mainly the command New-MsolUser, which takes a variety of parameters. 
A typical input file, will look like this:

 

1. Connect to your Office365 Tenant

Already covered in a previous post, you can review how you can setup and connect to your Office365 Tenant  PowerShell (Part 3) – Setting up your environment for Office365

Import-Module MSOnline
$Cred=Get-Credential 
Connect-MSolservice -Credential $Cred

2. Import the CSV File

$users=import-csv 'C:\PowerShell\users.csv'

foreach ($user in $users) {
$FirstName = $user.FirstName
$LastName = $user.LastName
$Country = $user.Country
$City = $user.City
$Title = $user.Title
$Department = $user.Department
$Office = $user.Office
....
}

You can review this post, to know more about how to feed files to your PowerShell Script PowerShell (Part 2) – Feeding files

3.Create the users and set the properties

Now, we need to loop over each row, get the column values, which will be used later to create the users using the New-MSolUser commandlet.

foreach ($user in $users) {
$FirstName = $user.FirstName
$LastName = $user.LastName
$Country = $user.Country
$City = $user.City
$Title = $user.Title
$Department = $user.Department
$Office = $user.Office

$DisplayName= $LastName + ", " + $FirstName
$UserName = $FirstName+"." + $LastName+"@logisam.net"
New-MsolUser -DisplayName $DisplayName -UserPrincipalName $UserName -FirstName $FirstName -LastName $LastName -Country $Country -City $City -Title $Title -UsageLocation $Country -LicenseAssignment "logisam:POWER_BI_STANDARD"
}

Here is the output you get once you run this script.

As you can see, the passwords for these accounts will be in the output screen.

If you log back to your Office365 Admin portal, you will see your newly created and licensed users.