Skip to content
Category: Azure
2020-03-20

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
  • mail
  • 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

Complete este formulario para recibir la guía de Windows Server en Azure
*Obligatorio
Complete este formulario para recibir la guía de Windows Server en Azure
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir 4 best practices to implement a comprehensive Zero Trust security approach
*Obligatorio
Complete este formulario para recibir 4 best practices to implement a comprehensive Zero Trust security approach
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir Cloud Migration Essentials
*Obligatorio
Complete este formulario para recibir Cloud Migration Essentials
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir Cloud security Advice for Nonprofit Leaders
*Obligatorio
Complete este formulario para recibir Cloud security Advice for Nonprofit Leaders
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir Prevent data leaks with Microsoft 365 Business Premium
*Obligatorio
Complete este formulario para recibir Prevent data leaks with Microsoft 365 Business Premium
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete this form to recieve the guide of Windows Server on Azure
*Required
Complete this form to recieve the guide of Windows Server on Azure
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve 4 best practices to implement a comprehensive Zero Trust security approach
*Required
Complete this form to recieve 4 best practices to implement a comprehensive Zero Trust security approach
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Cloud Migration Essentials
*Required
Complete this form to recieve Cloud Migration Essentials
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Cloud security Advice for Nonprofit Leaders
*Required
Complete este formulario para recibir Cloud security Advice for Nonprofit Leaders
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Prevent data leaks with Microsoft 365 Business Premium
*Obligatorio
Complete this form to recieve Prevent data leaks with Microsoft 365 Business Premium
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Cloud Migration Simplified Ebook.
*Obligatorio
Complete this form to recieve Prevent data leaks with Microsoft 365 Business Premium
Thank you for filling out the form [name]. Here's your Free eBook!