This script can be used by an Office365 administrator to batch add user information from a CSV file.
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.
This script contains the following functions:
You can use these functions in the following ways:
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.
#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 users
Import-Csv $FilePath | foreach{
Remove-MsolUser -UserPrincipalName $_.UserName -Force
}
#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 }


