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.
Get-ThrottlingPolicy
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
Get-MailboxExportRequest
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 "user@example.com" -AND $_.EventId -eq "receive"}
Get-MessageTrackingLog -Sender "user@example.com" -Recipients "user@example.com" -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 https://ps.outlook.com/powershell-liveid?DelegatedOrg=TENANTDOMAIN.onmicrosoft.com -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 '*example.net*'} | 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 olduser24912@example.com -NewUserPrincipalName olduser@example.com
Copying membership from a hybrid group to a cloud only group.
$credential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid?DelegatedOrg=TenantDomainNameHere.onmicrosoft.com -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
Get-WindowsUpdate
Install-WindowsUpdate
Restart-Computer
Reassociate PC to domain without reboot
Test-ComputerSecureChannel -credential DOMAIN\AdminAccount -Repair