Sample PowerShell Scripts for Office 365 Deployment
Applies to: Office 365 Deployment Guide for Enterprises
Topic Last Modified: 2012-05-13
Windows PowerShell sample scripts can be used to automate a number of deployment tasks. The following pages describe the scripts, the intended action, and associated arguments needed to successfully run each script.
Important: |
|---|
| This code and any associated information is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The entire risk of use, inability to use, or results from the use of this code remain with the user. |
Before you run the helper scripts, the following conditions must be met:
-
You must download and install the helper scripts from the Microsoft download center.
-
The user executing these scripts must have administrator-level Office 365 credentials.
AssignLicenseByDG is an example script which can be used to assign licenses. The assignment is based on a membership within a specified distribution group that aligns with Office 365 license type. The license types currently available in Office 365 are:
-
ENTERPRISEPACK
-
DESKLESSWOFFPACK
-
DESKLESSPACK
-
LITEPACK
You can use the Office 365 Get-MsolAccountSku cmdlet to obtain a list of license SKUs available to you based on your subscription type. For more information, see Windows PowerShell cmdlets for Office 365 . To use the script effectively, create the distribution groups using the license type as the distribution group name. License assignment and management can then be administered through membership in the associated group. The script uses the specified license type as the base for a distribution group lookup to the Online directory. It then iterates through each member of that group, assigning the specified license to each member. If a member already has a license assigned, it displays an informational message. If the member has a different license, the script reassigns that member to the specified license.
| Argument | Value |
|---|---|
| ‑Credential | Specifies the credential to use when connecting to the Office 365 PowerShell web service using Connect-MsolService, and when connecting to Exchange Online(https://ps.outlook.com/powershell). |
| ‑DGName | Specifies the license name associated with a distribution group maintained within the on-premises directory. Accepts one of the following values:
|
GetMsolTenantSkuUsage establishes a connection with the Office 365 provision web service API and collects information about the number of licenses owned, consumed, and available. The results are displayed in the PowerShell console and saved to a comma-separated values (CSV) file.
If a credential is specified, it is used to establish a connection with the provisioning web service API.
If a credential is not specified, an attempt is made to identify an existing connection to the provisioning web service API. If an existing connection is identified, the existing connection is used. If an existing connection is not identified, the user is prompted for credentials so that a new connection can be established.
| Argument | Value |
|---|---|
| ‑Credential | Specifies the credential to use when connecting to the Office 365 PowerShell web service using Connect-MsolService, and when connecting to Exchange Online (https://ps.outlook.com/powershell). |
| ‑OutputFile | Specifies the name of the output file. The argument can be the full path, including the file name, or the path to the folder in which to save the file. A default file name is used in the format |
GetMsolUserReport generates a CSV report containing both general and mailbox-related information about users in Office 365.
General User Information
-
UserPrincipalName
-
UpnSuffix
-
RecipientType
-
PrimarySmtpAddress
-
AdditionalSmtpAddresses
-
DisplayName
-
FirstName
-
LastName
-
BlockCredential
-
WhenCreatedUTC
-
WhenChangedUTC
-
LastDirSyncTime
-
UsageLocation
-
Company
-
Department
-
Manager
-
Title
-
StreetAddress
-
City
-
State
-
ZipCode
-
Office
-
TelephoneNumber
Licensing information
-
IsLicensed
-
LicenseReconciliationNeeded
-
AssignedLicenses
Mail-enabled User Information
-
EmailAddresses
-
PrimarySmtpAddress
-
UseMapiRichTextFormat
-
ExternalEmailAddress
-
HiddenFromAddressListsEnabled
Mailbox-Enabled Users
-
General information:
-
EmailAddresses
-
HiddenFromAddressListsEnabled
-
MaxSendSize
-
MaxReceiveSize
-
RetentionPolicy
-
RetentionHoldEnabled
-
StartDateForRetentionHold
-
EndDateForRetentionHold
-
RetainDeletedItemsFor
-
SingleItemRecoveryEnabled
-
LitigationHoldEnabled
-
LitigationHoldDate
-
ManagedFolderMailboxPolicy
-
DeliverToMailboxAndForward
-
ForwardingAddress
-
ForwardingSmtpAddress
-
EmailAddresses
-
Access types enabled:
-
ActiveSync
-
OWA
-
POP
-
IMAP
-
MAPI
-
ActiveSync
-
Mailbox statistics:
-
TotalItemSize
-
ItemCount
-
TotalDeletedItemSize
-
DeletedItemCount
-
TotalItemSize
-
ActiveSync devices:
-
MobileDeviceFriendlyName
-
MobileDeviceID
-
MobileDeviceModel
-
MobileDeviceOS
-
MobileDeviceOSLanguage
-
MobileDeviceLastSuccessSync
-
MobileDevicePolicyApplied
-
MobileDeviceFriendlyName
If the script is run without passing any arguments, it stores the results file on the local directory, dynamically prompting for credentials. Optionally, you can pass the following arguments, either separately or combined:
| Argument | Value |
|---|---|
| ‑Credential | Specifies the credential to use when connecting to the Office 365 PowerShell web service using Connect-MsolService, and when connecting to Exchange Online (https://ps.outlook.com/powershell). |
| ‑OutputFile | Specifies the name of the output file. The argument can be the full path, including the file name, or the path to the folder in which to save the file. A default file name is used in the format |
Get-SyncedObjectCount returns the number of objects that have been synchronized with Office 365, categorized by users, groups, and contacts. The process logic is as follows:
-
If the MsOnline module is not loaded, load the module.
-
Test for an existing connection, or establish a new connection.
-
Query sync objects and filter based on LastDirSyncTime.
-
Display filtered results count.
This script searches Active Directory and calculates the total number of users, groups, and contacts for each domain in the forest. Object type totals are written to the PowerShell console and saved to a CSV file.
| Argument | Value |
|---|---|
| ‑Credential | Specifies the username and password required to perform the operation in the on-premises directory. |
| ‑ForestName | Specifies the DNS name of the Active Directory forest. If a forest name is not specified, the current user context of the PowerShell session is used. |
| ‑OutputFile | Specifies the name of the output file. The argument can be the full path, including the file name, or the path to the folder in which to save the file. A default filename is used in the format |
NewDirSyncPowerShellShortcut examines the registry to verify that Windows Azure Active Directory Sync tool has been installed. If Directory Sync tool is installed, the script creates a file in the installation folder identified in the registry as DirSyncManagementShell.ps1.The script then creates a PowerShell shortcut in the start menu that points to this file.
NewRandomPasswordFile is a helper routine that generates a random password by referencing a CSV file. The CSV file should contain a list of user's User Principal Names (UPNs), such as someone@example.com. The script creates a new CSV file containing the original UPN and new password. Office 365 PowerShell commands can be used to generate the CSV argument file using the following example command line:
Get-MsolUser -All | Select-Object userprincipalname | export-csv results.csv -NoTypeInformation
| Argument | Value |
|---|---|
| ‑CsvFile | Specifies the path and file name of the CSV file. |
| ‑OutputFile | Specifies the name of the output file. The argument can be the fill path, including the file name, or the path to the folder in which to save the file. The default file name is used in the format |
SetMsolUserPasswordFromCSV is a helper routine that sets the cloud identity password. It references a CSV file and uses the set-MsolUserPassword cmdlet that is included in the MSOnline Module for Windows PowerShell. Use the -CreateCsvFileTemplate parameter to create a CSV template with all required parameters and example values. The CSV file must contain the UPN of the cloud identity along with the new password. Optionally, a third column in the CSV file can be used to set the ForceChangePassword setting on the identity. If the value of this column is left blank for a particular identity, the script does not request a password change during the next logon.
| Argument | Value |
|---|---|
| ‑CsvFile | Specifies the path and file name of the CSV file. |
| ‑OutputFile | Specifies that an example CSV file template should be created. |

Important: