Handy 365 PowerShell Commands

  • Post author:
  • Post category:Microsoft

Run all of these commands from PowerShell.

Connect to an Office 365 Tenant

This supports modern authentication and MFA

Set-ExecutionPolicy RemoteSigned
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline

Enable/Disable Azure AD Sync Scheduler

Ran these commands if you want to stop or start Azure AD Sync from running per its schedule.

To disable:

Set-ADSyncScheduler -SyncCycleEnabled $false

To enable :

Set-ADSyncScheduler -SyncCycleEnabled $true

Start Azure AD Sync Cycle

Azure AD Sync is configured to sync changes from AD to Azure on a regular basis (every thirty minutes by default), but you can kick it of manually if you need to sync changes immediately.

Running it with delta syncs changes since the last sync:

Start-ADSyncSyncCycle -PolicyType Delta

Running without passing it a PolicyType also runs a delta sync:

Start-ADSyncSyncCycle

Running it with a PolicyType of Initial syncs all objects and attributes within scope:

Start-ADSyncSyncCycle -PolicyType Initial

Give a Account Full Access to All Mailboxes in Office 365

Give a 365 user full access to all mailboxes, except for the “Admin” user. Change [email protected] to the user that you want to grant permissions to. The user that is granted access does not need to have license or mailbox.

Get-Mailbox -ResultSize unlimited -Filter {(RecipientTypeDetails -eq 'UserMailbox') -and (Alias -ne 'Admin')} | Add-MailboxPermission -User [email protected] -AccessRights fullaccess -InheritanceType all

Empty 365 Recycle Bin

Run the following from PowerShell to connect to the 365 tenant:

Connect-MSOLService -Credential $cred

Run this to list deleted users to make sure there’s nothing that you want to keep.

Get-MsolUser –ReturnDeletedUsers

Run this to permanently delete everything from the Recycle Bin. There is no going back from this unless you restore from backup.

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

Run this to delete one user from the Recycle Bin:

Remove-MsolUser –UserPrincipalName [username] –RemoveFromRecycleBin –Force

Change a Mailbox Type

Use the following command to convert a mailbox to one of the four types: Regular, Room, Equipment or Shared. Replace the email address with the address of the mailbox you want to convert.

Get-Mailbox -Identity [email protected] | Set-Mailbox -Type Shared

Find a Domain on any mailboxes in the tenant

Get-Mailbox -ResultSize unlimited | where {$_.emailAddresses -like "*@acme.com" }

Jason

Jason works as a project specialist at an IT MSP in Pennsylvania, USA. He's an IT/security professional with 20 years experience.