Office 365 - PowerShell Script to Verify User Already Exists or Not

Background

We have Office 365 installed for one of our customers. There were more than 1000 users already. The customer provided a list of users to be added to Office 365. But the problem is that in the list there were a few users that already existed. So we need to write a PowerShell script to determine if a user already exists, if not then add the user.

We can directly upload Excel to Office 365 and exclude existing users, new users will be created but our customer also wants a different password for each of the new users and doesn't want it to be changed when they login the first time. If we specify a password column to Excel, Office 365 doesn't accept it. Office 365 shows the result as in Figure 1.

Office 365 result when we upload excel with Password column
Figure 1: Office 365 result when we upload Excel with Password column

If we view the log file then it shows the error as in Figure 2.
Log File showing error
Figure 2: Log File showing error

So we don't have an option other than to write a PowerShell script.

Solution

Initially my thought was that its an easy job since we could easily create new users with the password we want using the -Password and -ForceChangePassword options as $false as in the following:

Import-Csv “D:\Users.csv” | Foreach-Object {New-MsolUser -UserPrincipalName $_. UserName -DisplayName $_.DisplayName -Password $_.Password -ForceChangePassword $false}

But for me it took some time to verify whether or not the user already existed using PowerShell. Therefore, I wrote this article.

The following is the procedure (if you are a newbie, please read my previous article):

  1. Set the Path of the users Excel file.

    $path = "D:\Users.csv"

  2. Connect to Office 365 as in the following:

    $cred=Get-Credential

    Connect-MsolService -Credential $cred

  3. Check if the user already exists:

    Get-MsolUser -UserPrincipalName $_.UserName -ErrorAction SilentlyContinue -ErrorVariable errorVariable

The preceding command is very important, Get-MsolUser gets the existing users based on user name. The important parameters are -ErrorAction and -ErrorVariable. If the given user does not exist then this command throws an exception but since we are executing the command with the SilentlyContinue option, it doesn't break the flow and the error is copied into the error variable “errorVariable” that we specified with the ErrorVariable parameter. So here we have a chance to check the errorVariable whether it is null or contains an error. If it is null then it means the user does not exist as in the following:

  1.  Import-Csv $path | Foreach-Object {   
  2. if($errorVariable -ne $null){  
  3.          Write-Host "Creating new user"  
  4.   
  5.           New-MSOLUser -UserPrincipalName  $_.UserName -DisplayName $_.DisplayName -Password  $_.Password -FirstName $_.FirstName -LastName $_.LastName -Department $_.Department -City $_.City -Country $_.Country_or_Region -MobilePhone $_.MobilePhone -PhoneNumber $_.OfficePhone -State $_.State_or_Province -StreetAddress $_.Address -Title $_.JobTitle  -Office $_.OfficeNumber -Fax $_.Fax -PostalCode $_.ZIP_or_Postal_Code -ForceChangePassword $false -ErrorVariable errorVariable   
  6.                   
  7.     }  
  8.   else{  
  9.           Write-Host "User already exists : " $_.UserName  
  10.        }   
  11. }#Foreach-object  
References 

Thanks!

Up Next
    Ebook Download
    View all
    Learn
    View all