{"id":6403,"date":"2022-11-25T23:21:15","date_gmt":"2022-11-25T23:21:15","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=6403"},"modified":"2022-11-25T23:21:16","modified_gmt":"2022-11-25T23:21:16","slug":"bulk-remove-domain-from-users-in-365-upn-and-smtp","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/bulk-remove-domain-from-users-in-365-upn-and-smtp","title":{"rendered":"Bulk Remove Domain from users in 365 ( UPN and SMTP )"},"content":{"rendered":"\n<p>Remove Domain from UPN<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Connect-MsolService\n$domain = \"domain.com\"\n$TenantDomain = @((Get-MsolDomain |?{$_.Name -match '.onmicrosoft.com' -and $_.Name -notmatch '.mail.'}).Name)?\n$msolUsersWithDomain = @(Get-MsolUser -All | ? {$_.UserPrincipalName -match $domain})?\n#Remove UPN\n$msolUsersWithDomain | % {Set-MsolUserPrincipalName -ObjectId $_.objectId -NewUserPrincipalName ($_.UserPrincipalName.Split(\"@\")&#91;0]+ \"@\" +$TenantDomain );}<\/code><\/pre>\n\n\n\n<p>Remove SMTP address<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Connect-ExchangeOnline\n$domain = \"domain.com\"\n$MailboxeswithDomain = @(Get-Recipient -ResultSize Unlimited | where {$_.PrimarySmtpAddress -match \"$Domain\" -or $_.EmailAddresses -match \"$Domain\"} | select PrimarySmtpAddress,EmailAddresses,RecipientType,RecipientTypedetails | Sort-Object -Property RecipientType,RecipientTypedetails)?\n\nforeach ($Mailbox in $MailboxeswithDomain) {  \r\n    $Mailbox.EmailAddresses | Where-Object { ($_ -clike \"smtp*\") -and ($_ -like \"*@$domain\") } | \r\n\r\n    # Perform operation on each item\r\n    ForEach-Object {\r\n\r\n        # Remove the -WhatIf parameter after you tested and are sure to remove the secondary email addresses\r\n        Set-Mailbox $Mailbox.DistinguishedName -EmailAddresses @{remove = $_ } -WhatIf\r\n\r\n        # Write output\r\n        Write-Host \"Removing $_ from $Mailbox Mailbox\" -ForegroundColor Green\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Remove Domain from UPN Remove SMTP address<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1206,2036,576,4098],"class_list":["post-6403","post","type-post","status-publish","format-standard","hentry","category-research","tag-1206","tag-bulk","tag-powershell","tag-upn"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=6403"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6403\/revisions"}],"predecessor-version":[{"id":6404,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6403\/revisions\/6404"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=6403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=6403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=6403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}