Batch import user information from a CSV file

Introduction

This script can be used by an Office365 administrator to batch add user information from a CSV file.

Scenarios

Sometimes an Office365 administrator must add many users to Microsoft Office365. For example, when an organization first uses Office365, they must import all members to the system.

Script

This script contains the following functions:

You can use these functions in the following ways:

  1. Run the Import-Module cmdlet to import a module file.
    Import-Module filepath\scriptname.psm1
  2. Use the Enter-OSCOffice365 function to log on to the Office365.
    Enter-OSCOffice365 -UserId<your user id>
  3. Use the Import-OSCUser or Remove-OSCUser functions to batch add or remove users to and from Office365.
    Import-OSCUser -FilePath <your CSV file path>
    Remove-OSCUser -FilePath <your CSV file path>

Note You must prepare a CSV file that uses columns that resemble the following:

UserName(required): The user ID That You used to log on to the Office365. For example: username@domain. 
DisplayName(required): The display name of the user.
Department: The user’s department.
JobTitle: The job title of the user.
OfficeNumber: The user’s office number.
OfficePhone: The user’s office telephone number.
MobilePhone: The user’s mobile phone number.
FaxNumber: The user’s fax number.
StreetAddress: The user’s address information.
City: The user’s city information.
StateOrProvince: The user’s state or province information.
ZipOrPostalCode: The ZIP Code/Postal Code or postal code of the city.
CountryOrRegion: The user’s country information.
BlockCredential: This must be true or false. If true, the user will be unable to sign in or access services.
UsageLocation: This field is required if the LicenseAssignment is true. You must use a tow-letter
uppercase country code. For example,” US” is for the United States.
LicenseAssignment: This field must be true or false. It is used to determine whether to assign the license to the user.

Here are some code snippets for your references.To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
#Import all users information from .CSV file     Import-Csv$FilePath|foreach{              
    Add-UserToO365 -UserName $_.UserName -DisplayName $_.DisplayName -Department $_.Department -JobTitle $_.JobTitle -OfficeNumber $_.OfficeNumber ` 
    -OfficePhone $_.OfficePhone -MobilePhone $_.MobilePhone -FaxNumber $_.FaxNumber -StreetAddress $_.StreetAddress -City $_.City -StateOrProvince $_.StateOrProvince -ZipOrPostalCode ` 
    $_.ZipOrPostalCode -CountryOrRegion $_.CountryOrRegion -BlockCredential $_.BlockCredential -UsageLocation $_.UsageLocation -LicenseAssignment $_.LicenseAssignment           
} 
  
#Remove all users information contained in the .CSV file from Office 365 active usersImport-Csv$FilePath|foreach{  
    Remove-MsolUser -UserPrincipalName $_.UserName -Force 
}

Examples

Example 1: Display help about the Enter-OSCOffice365 function.
Command: Get-Help Enter-OSCOffice365
Screenshot:

 
Example 2: Log on to the Office365 by using the Enter-OSCOffice365 function.
Command: Enter-OSCOffice365 –UserId <your user id>
Screenshot:

 Example 03: Display help about the Import-OSCUser function.
Command:
Get-Help Import-OSCUser
Screenshot:

 
Example 04: Import user information to Office365 from the CSV file.
Command:
Import-OSCUser –FilePath  <your CSV file path>
Screenshot:

 
Example 05: Display help about Remove-OSCUser.
Command:
Get-Help Remove-OSCUser
Screenshot:

Example 06: Remove user information for Office365 from the CSV file.
Command:
Remove-OSCUser –FilePath <your CSV file path>
Screenshot:

 

Prerequisite

Windows PowerShell 2.0

Additional Resources

Related forum threads:
http://community.office365.com/en-us/forums/150/t/59687.aspx
http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh124998.aspx
http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh125002.aspx