Useful Powershell Snippets

On-premise Exchange

Adding Send-As rights

get-user -identity MAILBOX_NAME | Add-ADPermission -User USER_ACCOUNT -ExtendedRights Send-As

Setting maximum number of active sync devices. Typically this only comes up if you’re sharing an account across a fair number of devices.

Set-ThrottlingPolicy EASMaxDevices 25 Identity DefaultThrottlingPolicy_XXXXXXXXX

Exporting mailboxes directly to PST. You only need to assign Management Role Assignment once per admin account.

New-ManagementRoleAssignment -Role "Mailbox Import Export" -User "ADMIN_ACCOUNT"
New-MailboxExportRequest -Mailbox MAILBOX_NAME -FilePath \\ServerName\pst\username.pst

Finding emails by email address

get-transportserver | Get-MessageTrackingLog -Start "1/01/2017 12:00:00 am" -End "2/21/2017 17:30:00 pm" -resultsize unlimited |where-object {$_.Recipients -like "" -AND $_.EventId -eq "receive"}
Get-MessageTrackingLog -Sender "" -Recipients "" -Start "1/19/2015 8:00AM" | FL Sender,Recipients,MessageSubject,MessageId

Setting up email forwarding

Set-MailboxAutoReplyConfiguration -identity USER_NAME -AutoReplyState enabled -ExternalAudience all -InternalMessage "Internal out of office message." -ExternalMessage "External out of office message."
Set-Mailbox USER_NAME -ForwardingAddress USER_NAME -DeliverToMailboxAndForward $True

Getting list of all the mailboxes and find the mailbox size

$Mailboxes = Get-Mailbox -ResultSize 200
foreach ($Mailbox in $Mailboxes)
$Mailbox | Add-Member -MemberType "NoteProperty" -Name "MailboxSizeMB" -Value ((Get-MailboxStatistics $Mailbox).TotalItemSize.Value.ToMb())
$Mailboxes | Sort-Object MailboxSizeMB -Desc | Select DisplayName, Alias, PrimarySMTPAddress, MailboxSizeMB | Export-Csv -NoType "C:\Temp\Mailboxes.csv"

O365 Powershell commands

Updating O365 time zone and language in bulk

$credential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $Session
# Set all mailboxes to en-US, EST
get-mailbox | Set-MailboxRegionalConfiguration -Language 1033 -TimeZone "Eastern Standard Time"
# Find all mailboxes that match
get-mailbox -filter {EmailAddresses -like '**'} | Get-MailboxRegionalConfiguration
get-mailbox -filter {EmailAddresses -like '*bob*'} | Get-MailboxRegionalConfiguration
# Set a mailbox to en-US, EST
get-mailbox -filter {EmailAddresses -like '*bob*'} | Set-MailboxRegionalConfiguration -Language 1033 -TimeZone "Eastern Standard Time"

Changing user name. Usually needed if AD Sync is being wonky, which is fairly common.

$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
Set-MsolUserPrincipalName -UserPrincipalName -NewUserPrincipalName

Copying membership from a hybrid group to a cloud only group.

$credential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$OldMembers = (Get-DistributionGroupMember "ExampleGrpHere").Name
New-DistributionGroup Cloud-ExampleGrpHere -Type Distribution -Members $OldMembers

Windows Powershell commands

Running updates on a new PC

Powershell.exe -ExecutionPolicy Unrestricted
Install-Module PSWindowsUpdate

Reassociate PC to domain without reboot

Test-ComputerSecureChannel -credential DOMAIN\AdminAccount -Repair