How to Register Users in Office 365 with Powershell
As many of you already know, the Office365 platform can perform tasks automatically, which means simplification and time savings.
In today’s post I will explain how to register users in Office365 , as well as their allocation of licenses with PowerShell. In this way, it will be much more comfortable to register users massively than if we did it manually.
Next, I’ll show you the two steps we’re going to follow:
1.Create a CSV File
First, we will create a CSV file with the following name “CreaciónUsusuarioOffice365.csv” and with this data (Here you have the template to download)
- DisplayName
- userPrincipalName
- passwordProfile
- GiveName
- Surname
- JobTitle
- Departament
- UsageLocation
- streetAddress
- state
- country
- physicalDeliveryOfficeName
- city
- postalCode
- telephoneNumber
- License
- MailNickName
*Note: I have left the link so you can download the file and only have to add the data of the users you want to register in Office365
Here is an example of what the completed data file should look like
2. Script Execution
Once the data is inserted in the file “CreateUserOffice365.csv” we will save it in the My Documents folder.
When we have it saved we proceed to Execute the Script “CreacionUsuarios_V2.ps1“.
$date = Get-Date -UFormat "%Y%m%dT%H%M%S"
$DesktopUser = $env:USERNAME
$File = "C:\Users\" + $DesktopUser + "\Documents\CreacionUsuarioOffice365.csv"
$PasswordProfile= New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$CredO365AD = Get-Credential
Connect-AzureAD -Credential $CredO365AD
Connect-MsolService -Credential $CredO365AD
Function LEERUsuarios
{
$ReadF = Import-Csv -Path $File -Delimiter ";" -Encoding ASCII
foreach($user in $ReadF)
{
$user.userPrincipalName
IF(Get-AzureADUser -All $true | ? {$_.userPrincipalName -eq $user.userPrincipalName})
{
Write-Host "El usuario exite..." -ForegroundColor Yellow
Modificar
}
Else
{
Write-Host "`n`tEl usuario no existe" -ForegroundColor Green
$USer.DisplayName
$PasswordProfile.Password = $user.passwordProfile
$USer.userPrincipalName
$user.accountEnabled
$user.GiveName
$user.Surname
$user.JobTitle
$user.country
$user.city
$user.postalCode
New-AzureADUser -DisplayName $USer.DisplayName -PasswordProfile $PasswordProfile -UserPrincipalName $USer.userPrincipalName -AccountEnabled $true -GivenName $user.GiveName -Surname $user.Surname -JobTitle $user.JobTitle -Country $user.country -City $user.city -PostalCode $user.postalCode -MailNickName $user.MailNickName
}
}
}
Function Modificar
{
Write-Host "Se va a actualizar el Usuarios" -ForegroundColor DarkYellow
$GetLicense = Get-MsolUser -UserPrincipalName $USer.userPrincipalName
foreach($Licenses in $GetLicense)
{
IF($licenses.IsLicensed -eq $false)
{
Write-Host "`n`t******************" -ForegroundColor Green
$Licenses.UserPrincipalName
$licenses.IsLicensed
$user.License
Set-MsolUserLicense -UserPrincipalName $Licenses.UserPrincipalName -AddLicenses $user.License
Write-Host
}
Else
{
Write-Host "`t`t*****************************" -ForegroundColor White
$Licenses.UserPrincipalName
$licenses.IsLicensed
Write-Host "`t`t*****************************`n" -ForegroundColor White
}
IF($Licenses.UsageLocation -eq $Null)
{
Write-Host "`n`t******************" -ForegroundColor Cyan
Set-Msoluser -UserPrincipalName $USer.userPrincipalName -UsageLocation $user.country
Get-MsolUser -UserPrincipalName $USer.userPrincipalName | where {$_.UsageLocation -eq $null}
}
Else
{
Write-Host "`n`t******************" -ForegroundColor DarkMagenta
$Licenses.UsageLocation
Get-MsolUser -UserPrincipalName $USer.userPrincipalName | where {$_.UsageLocation -ne $null}
}
}
}
LEERUsuarios
While the script is running we can see the result of the “Active Users” management console of Office365
If you want us to help your business or company contact us at info@aleson-itc.com or call us at +34 962 681 242
Consultor en plataformas Microsoft con más de 20 años de experiencia y con las de 15 años de experiencia en Microsoft Sharepoint