PowerShell

Microsoft 365 PowerShell scripting is a powerful tool for automating and managing various aspects of the Microsoft 365 environment. With PowerShell, IT professionals can streamline administrative tasks, enhance operational efficiency, and ensure consistent configurations across the organization.

8/1/20202 min read

PowerShell

Modify Mailbox Settings Via the Set-Mailbox Cmdlet

Set-Mailbox -Identity "Joseph Ludwig" -Alias "Josephludwig25"

Set-Mailbox -Identity “Joseph Ludwig” -DisplayName “Joe Ludwig”

Set-Mailbox -Identity “Joseph Ludwig” -samAccountName joeludwig

Set-Mailbox -Identity “Joseph Ludwig” -ArchiveDatabase Archive01

Set-Mailbox -Identity “Joseph Ludwig” -Database MBX01Archive01

Set-Mailbox -Identity “Joseph Ludwig” -ArchiveQuota 50GB

Set-Mailbox -Identity “Joseph Ludwig” -ArchiveWarningQuota 45GB

Set-Mailbox -Identity “Joseph Ludwig” -ForwardingAddress “Joseph Ludwig”

Set-Mailbox -Identity “Joseph Ludwig” -ForwardingSmtpAddress “josephludwig@gmail.com

Set-Mailbox -Identity “Joseph Ludwig” -ForwardingAddress “Joseph Ludwig” -DeliverToMailBoxAndForward $True

Get-AdUser -Filter {department eq 'IT'} | ForEach {Set-Mailbox -Identity $_.Name -DeliverToMailboxAndForward $true -ForwardingAddress "Joseph Ludwig"}

Get-ADUser -SearchBase "OU=IT,OU=Department,DC=jl,DC=joe,DC=com" -Properties -Filter | Select samAccountName,GivenName,Surname,DisplayName,UserPrincipalName,EmailAddress,Department,msExchRecipientTypeDetails,Title,Company,Enabled,LastLogonDate,PasswordLastSet,PasswordNeverExpires | Export-Csv C:\Users\joe\Desktop\HR10102024.csv -Encoding UTF8 -NoTypeInformation

Sharepoint Cmdlet

Get-SPOTenant | Select disallowinfectedfiledownload

Set-SPOTenant -DisallowInfectedFileDownload $true

Office 365 Tenant Cmdlet

New-AuthenticationPolicy -Name "Block Basic Authentication"

Set-OrganizationConfig -MailTipsExternalRecipientsTipsEnabled $True

Set-OwaMailboxPolicy OwaMailboxPolicy-Default -LinkedInEnabled $False

Set-OwaMailboxPolicy OwaMailboxPolicy-Default -AdditionalStorageProvidersAvailable $False

Set-AdminAuditLogConfig $true

Get-MsolcompanyInformation | Select Allowemailverifiedusers, Allowadhocsubscriptions

Get-OrganizationConfig | fl auditdisabled*

Disable Mailbox

Disable-Mailbox it@josephludwig.com

Delete mailbox

Remove-Mailbox it@josephludwig.com

Verify Deleted Mailbox

$dbs = Get-MailboxDatabase

$dbs | foreach {Get-MailboxStatistics -Database $_.DistinguishedName} | where {$_.DisplayName -eq "<DisplayName>"} | Format-List DisconnectReason,DisconnectDate

Reconnect deleted Mailbox

New-MailboxRestoreRequest -SourceStoreMailbox e4890ee7-79a2-4f94-9569-91e61eac372b -SourceDatabase MBXDB01 -TargetMailbox "Joe Ludwig" -AllowLegacyDNMismatch

Get-User -Identity it@josephludwig.com

Connnect Mailbox:

Connect-Mailbox -Identity "Joseph Ludwig" -Database MBXDB02 -LinkedDomainController DC01 -LinkedMasterAccount it@josephludwig.com -Alias joeludwig

Enable Audit Log:

Set-AdminAuditLogConfig -UnifiedAuditLogIngestionEnabled $true

Get-AdminAuditLogConfig | Format-List UnifiedAuditLogIngestionEnabled

Set-AdminAuditLogConfig -UnifiedAuditLogIngestionEnabled $false

One Drive Provisioning

Install-Module Microsoft.Online.SharePoint.PowerShell -Force

Connect-SPOService -Url https://domain-admin.sharepoint.com

$emails = "joe1@contoso.com", "joe2@contoso.com"

Request-SPOPersonalSite -UserEmails $emails -NoWait

Enable Archive Mailbox (PowerShell command to automatically provision an archive mailbox when a primary mailbox that's licensed for archiving reaches 90% of the quota )

Enable-Organizationcustomization

Set-OrganizationConfig -AutoEnableArchiveMailbox $true

Enable for All

Get-Mailbox -Filter {ArchiveGuid -Eq "00000000-0000-0000-0000-000000000000" -AND DisabledArchiveGuid -Eq "00000000-0000-0000-0000-000000000000" -AND RecipientTypeDetails -Eq "UserMailbox"} | Enable-Mailbox -Archive

Enable for users doesn’t have archive mailbox enabled

Get-Mailbox -Filter {ArchiveStatus -Eq "None" -AND RecipientTypeDetails -eq "UserMailbox"} | Enable-Mailbox -Archive

Enable for Single User

Enable-Mailbox -Identity <username> -Archive

Disable Archive Mailbox

Disable-Mailbox -Identity <username> -Archive

Disable for All

Get-Mailbox -Filter {ArchiveGuid -Ne "00000000-0000-0000-0000-000000000000" -AND RecipientTypeDetails -Eq "UserMailbox"} | Disable-Mailbox -Archive

Autoexpand Archive for Org

Set-OrganizationConfig -AutoExpandingArchive

Get-OrganizationConfig | FL AutoExpandingArchiveEnabled

Expand Auto Archive for Single User

Enable-Mailbox <user mailbox> -AutoExpandingArchive

Get-Mailbox <user mailbox> | FL AutoExpandingArchiveEnabled

Disable-Mailbox -Identity "joseph.ludwig@josephludwig.com" -Confirm:$false

With CSV

Identity

john.doe@ josephludwig.com

jane.smith@josephludwig.com

mark.brown@contoso.com

Import-Csv -Path "C:\Path\To\mailboxes.csv" | ForEach-Object {

Disable-Mailbox -Identity $_.Identity -Confirm:$false

}

Without CSV

$mailboxes = @(

"john.doe@josephludwig.com",

"jane.smith@josephludwig.com",

"mark.brown@josephludwig.com"

)

foreach ($mailbox in $mailboxes) {

Disable-Mailbox -Identity $mailbox -Confirm:$false

}

Get-Mailbox -InactiveMailboxOnly | FL UserPrincipalName,AutoExpandingArchiveEnabled

Outlook Registry Key to Configure Outlook for On Prem user without connecting to office 365 when organization is in hybrid mode.

reg add HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\16.0\Outlook\AutoDiscover /v ExcludeExplicitO365Endpoint /t REG_DWORD /d 1 /f