Handy Office 365 PowerShell One-Liners

Handy Office 365 PowerShell One-Liners

  •  
  •  
  •  
  •  
  •  
  •  

Here are some handy one-liners that you may find useful when managing Office 365.

Ok, some of them are a few lines, but they’re still handy.

Connecting to things

Connect to the Microsoft Online Services interface for account management tasks.

import-module MSOnline
$cred = Get-Credential
Connect-MSOLService -credential $cred

Connect to the Microsoft Exchange Online interface for Exchange-related tasks.

$cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session

Identity Management

Set Office 365 passwords for all accounts to P@ssword1 and clear Change Password Flag (not valid for synced or federated customers)

Get-MsolUser -All | Set-MsolUser -NewPassword P@ssword1 -ForceChangePassword $False

Set Office 365 passwords for all accounts to never expire (not valid for synced or federated customers)

Get-MsolUser -All | Set-MsolUser -PasswordNeverExpires $True

Set Usage Location to United States for All users

Get-MsolUser | Set-MsolUser -UsageLocation "US"

Assign “Exchange Online Plan 1” License to All Users for organization TestOrg

Get-MsolUser -All | Set-MsolUserLicense -addlicenses "testorg:EXCHANGESTANDARD"

Force Removal of deleted mailboxes from Recycle Bin

Get-MsolUser -ReturnDeletedUsers | Remove-MsolUser -RemoveFromRecycleBin -Force

Mailbox Management

Set Time Zone to Eastern Time and Language to English (US) for all users

get-mailbox -Filter {RecipientTypeDetails -eq 'UserMailbox'} | Set-MailboxRegionalConfiguration -Language "en-US" -TimeZone "Eastern Standard Time" -DateFormat "M/d/yyyy" -TimeFormat "h:mm tt"

Get a user’s mailbox permissions on a selected mailbox

Get-MailboxPermission -Identity <mailbox@domain.com> | Where {_.User -like '*<mailbox@domain.com>*'}
Get-RecipientPermission -Identity <mailbox@domain.com> | Where {_.Trustee -like '*<mailbox@domain.com>*'}


Get a list of Directly-granted rights, excluding “SELF”

Get-Mailbox | Get-MailboxPermission | Where-Object { ($_.AccessRights -like '*full*') -and ($_.IsInherited -eq $false) -and -not ($_.User -like '*nt authority\self*') }
Get-Mailbox | Get-RecipientPermission | Where-Object { ($_.AccessRights -like '*send*') -and ($_.IsInherited -eq $false) -and -not ($_.User -like '*nt authority\self*') }

Set Shared Mailbox quota at 4.5GB

Get-Mailbox -RecipientTypeDetails SharedMailbox | Set-Mailbox -ProhibitSendQuota 4500MB -ProhibitSendReceiveQuota 5000mb -IssueWarningQuota 4400mb

Get Distribution Group Members

$Reports=@()
$Groups=Get-DistributionGroup
$Groups| foreach {
 $GroupName=$_.DisplayName
 $Report=Get-distributionGroupMember -identity $_.identity| select @{Name='Distribution Group'; Expression={[String]::join(";", $GroupName)}}, DisplayName, PrimarySmtpAddress
 $Reports=$Reports+$Report
 }
$Reports | Export-csv -NoType -Path .\"output.csv" -ErrorAction SilentlyContinue

Add Alias Domain to All Mailboxes (not valid for synced or federated customers)

$users = Get-Mailbox
$aliasdomain = "newdomain.com"
foreach ($a in $users) {$a.emailaddresses.Add("$($a.alias)@$aliasdomain")}
$users | %{Set-Mailbox $_.Identity -EmailAddresses $_.EmailAddresses}

Get All User Mailbox Sizes

Get-Mailbox -Resultsize Unlimited | Get-MailboxStatistics | Select-Object DisplayName,TotalItemSize

Convert User mailbox to Room Mailbox

Set-Mailbox -Identity <ConferenceRoom@domain.com> -Type Room
Set-MailboxFolderPermission -Identity ConferenceRoom@domain.com:\Calendar -user Default -AccessRights Author

Let me know if there are other tasks you might like to see demonstrated!

Published by Aaron Guilmette

Helping companies conquer inferior technology since 1997. I spend my time developing and implementing technology solutions so people can spend less time with technology. Specialties: Active Directory and Exchange consulting and deployment, Virtualization, Disaster Recovery, Office 365, datacenter migration/consolidation, cheese.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.