Use these common PowerShell Scripts for Data Retrieval

powershell logo

MessageOps has compiled these handy PowerShell scripts that you can use to get information on the objects within your Office 365 tenant.

Display a list of office 365 Global Administrators

PowerShell command syntax: Get-MsolRoleMember –RoleObjectId .ObjectId | Select DisplayName,EmailAddress

For example: Get-MsolRoleMember -RoleObjectId “Power Administrator”.ObjectId | Select DisplayName,EmailAddress

Display Different type of recipients with a specific Domain name (This is for the Administrator who need to administer more than one domain)

Users UPN with a specific domain name suffix

PowerShell command syntax: Get-MsolUser –DomainName | ft -Property UserPrincipalName

For example: Get-MsolUser -DomainName ictformyanmar.com | ft -Property UserPrincipalName

Display a list of user from a specific department

PowerShell command syntax: Get-msoluser | Where {$_.Department -eq “”}

For example: Get-msoluser | Where {$_.Department -eq “Mrkt”}

Display list of services assigned to user

PowerShell command syntax: Get-msoluser -all | ForEach-Object { “=============”; $_.DisplayName; $_.licenses[0].servicestatus }

Display list of users and their currently License

PowerShell command syntax: Get-MsolUser | Where-Object {$_.isLicensed -eq “TRUE”} | select Displayname, Licenses

Export list of users and their currently License to a file

PowerShell command syntax: Get-MsolUser | Where-Object {$_.isLicensed -eq “TRUE”} | select Displayname, Licenses < Path & File Name>

For example: Get-MsolUser | Where-Object {$_.isLicensed -eq “TRUE”} | select Displayname, Licenses > c:\temp folder\Licenses.txt

Export information about office 365 users mailbox to CSV file

PowerShell command syntax: Export list of all Mailboxes Get-mailbox | Export-Csv < Path & File Name>

For example: Get-mailbox | Export-Csv c:\temp folder\allmailboxes.csv

Export list of all office 365 users

PowerShell command syntax: Get-MsolUser | select DisplayName,FirstName,LastName,UserPrincipalName,MobilePhone,PhoneNumber,Office,Fax,StreetAddress,PostalCode,City,Country,State,Department, IsLicensed ,PreferredLanguage , Title , UsageLocation | Export-Csv c:\temp\hv.csv

For example: Get-MsolUser | select DisplayName,FirstName,LastName,UserPrincipalName,MobilePhone,PhoneNumber,Office,Fax,StreetAddress,PostalCode,City,Country,State,Department, IsLicensed ,PreferredLanguage , Title , UsageLocation | Export-Csv c:\temp folder\allmailboxesusage.csv

Export list of all office 365 users with License

PowerShell command syntax: Get-MsolUser | Where-Object {$_.isLicensed -eq “TRUE”} | Export-Csv < Path & File Name>

For example: Get-MsolUser | Where-Object {$_.isLicensed -eq “TRUE”} | Export-Csv c:\temp\userslicenses.csv

Export list of all office 365 users without License

PowerShell command syntax: Get-MsolUser -UnlicensedUsersOnly | Export-Csv < Path & File Name>

For example: Get-MsolUser -UnlicensedUsersOnly | Export-Csv c:\temp folder\userswithoutlicense.csv

Export list of all Distribution Groups

PowerShell command syntax: Get-DistributionGroup | Export-Csv < Path & File Name>

For example: Get-DistributionGroup | Export-Csv c:\temp folder\listofdg.csv

Get all email addresses from a non-licensed user

For example: get-msoluser -searchstring [email protected] | %{$_.proxyaddresses}

Get a list of softdelted mailboxes or lithold mailboxes

  1. Get-mailbox -softdeletedmailbox
  2. Get-mailbox -InactiveMailboxOnly
  3. You can look for a specific one with adding in the middle -identity [email protected]

Find Duplicate of SMTP Address

Used for when an SMTP address needs to be used, but the portal is showing it is already in use. This script finds where the SMTP is currently sitting so it can be modified.

# Define email to search for

$mail = “[email protected]

# Do the different searches (requires connect-msolservice)

Get-MsolGroup -All | where {$_.ProxyAddresses -match $mail }

Get-Msoluser -All | where {$_.ProxyAddresses -match $mail }

Get-Msoluser -ReturnDeletedUsers -All | where {$_.ProxyAddresses -match $mail }

Get-MsolContact -All | where {$_.EmailAddress -match $mail }

# Do the different searches (requires connection to Exchange online)

Get-Group -ResultSize Unlimited | where {$_.WindowsEmailAddress -match $mail }

Get-DistributionGroup | where {$_.EmailAddresses -match $mail }

Get-Mailbox -ResultSize unlimited | where {$_.EmailAddresses -match $mail }

Get-Mailbox -SoftDeletedMailbox | where {$_.EmailAddresses -match $mail }

Get-MailUser -ResultSize unlimited | where {$_.EmailAddresses -match $mail }

Get-User -ResultSize unlimited | where {$_.UserPrincipalName -match $mail }

Get-User -ResultSize unlimited | where {$_.WindowsEmailAddress -match $mail }

Get-MailContact -ResultSize Unlimited | where {$_.EmailAddresses -match $mail }

Get-Recipient -ResultSize Unlimited | where {$_.EmailAddresses -match $mail }

Get-MailPublicFolder -ResultSize unlimited | where {$_.EmailAddresses -match $mail }

Find Forwards

Shows all active forwards setup on a single mailbox.

get-mailbox -identity [email protected]| Select ForwardingSMTPAddress,DeliverToMailboxAndForward

Show Mailbox Sizes

Shows the mailbox sizes for all licensed mailbox users in the tenant and placed them into a CSV.

For example: Get-Mailbox -ResultSize unlimited | Get-MailboxStatistics | Select DisplayName,TotalItemSize | Sort-Object -Property TotalItemSize -Descending | export-csv -path C:\temp\sizes.csv

Mailbox Statistics

Pulls all the statistics for all active mailboxes in a tenant and places them into a CSV.

For example:

$MailboxData = @()

$mailboxes = Get-Mailbox -ResultSize unlimited

foreach ($mailbox in $mailboxes) {

$DBObject = new-object PSObject

$DBObject | add-member NoteProperty -Name Alias -Value $Mailbox.alias

$DBObject | add-member NoteProperty -Name OU -Value $Mailbox.organizationalunit

$DBObject | add-member NoteProperty -Name EmailAddress -Value $Mailbox.primarysmtpaddress

$DBObject | add-member NoteProperty -Name Items -Value (Get-MailboxStatistics $mailbox.alias).itemcount

$DBObject | add-member NoteProperty -Name StorageLimit -Value (Get-MailboxStatistics $mailbox.alias).storagelimitstatus

$DBObject | add-member NoteProperty -Name Size -Value (Get-MailboxStatistics $mailbox.alias).totalitemsize

$DBObject | add-member NoteProperty -Name ServerStore -Value (Get-MailboxStatistics $mailbox.alias).database

$DBObject | add-member NoteProperty -Name Database -Value (Get-MailboxStatistics $mailbox.alias).databasename

$MailboxData += $DBObject

}

$MailboxData | Export-Csv c:\temp\mailboxreport.csv

Was this article helpful?
YesNo