-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert-MailboxesToShared.ps1
More file actions
99 lines (79 loc) · 3.45 KB
/
Convert-MailboxesToShared.ps1
File metadata and controls
99 lines (79 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<#
.SYNOPSIS
Converts mailboxes of users in specified Active Directory OUs to shared mailboxes.
.DESCRIPTION
This script retrieves user accounts from one or more specified Organizational Units (OUs) in Active Directory,
filters those whose UserPrincipalName (UPN) ends with a specified domain suffix, and converts their Exchange Online
mailboxes to shared mailboxes.
.PARAMETER OUPaths
An array of distinguished names representing the OUs to search.
.PARAMETER DomainSuffix
The UPN suffix used to filter accounts (e.g., "@yourcompany.com").
.PARAMETER ShowExcluded
Switch to display UPNs that do not match the specified domain suffix.
.EXAMPLE
.\Convert-MailboxesToShared.ps1 -OUPaths "OU=Dept1,DC=domain,DC=local","OU=Dept2,DC=domain,DC=local" -DomainSuffix "@yourcompany.com"
.NOTES
- Requires ActiveDirectory and ExchangeOnlineManagement modules
- Must be run with sufficient permissions in both AD and Exchange Online
#>
param (
[Parameter(Mandatory = $true)]
[string[]]$OUPaths,
[Parameter(Mandatory = $true)]
[string]$DomainSuffix,
[Parameter()]
[switch]$ShowExcluded
)
# Ensure required modules are loaded
Import-Module ActiveDirectory -ErrorAction Stop
Import-Module ExchangeOnlineManagement -ErrorAction SilentlyContinue
# Check connection to Exchange Online
if (-not (Get-PSSession | Where-Object { $_.ConfigurationName -eq "Microsoft.Exchange" })) {
try {
Connect-ExchangeOnline -ErrorAction Stop
Write-Host "Connected to Exchange Online." -ForegroundColor Cyan
} catch {
Write-Host "Failed to connect to Exchange Online: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
}
# Collect all users from the provided OUs
$allUsers = foreach ($OU in $OUPaths) {
Get-ADUser -SearchBase $OU -Filter * -Property UserPrincipalName
}
Write-Host "Total users retrieved from specified OUs: $($allUsers.Count)" -ForegroundColor Cyan
# Separate matching and non-matching users
$matchingUsers = $allUsers | Where-Object { $_.UserPrincipalName -like "*$DomainSuffix" }
$nonMatchingUsers = $allUsers | Where-Object { $_.UserPrincipalName -notlike "*$DomainSuffix" }
# Optional: Show users who don't match the domain suffix
if ($ShowExcluded) {
Write-Host "`nUPNs excluded (do not match '$DomainSuffix'):" -ForegroundColor Yellow
$nonMatchingUsers | ForEach-Object {
Write-Host $_.UserPrincipalName
}
}
# Summary
Write-Host "`nTotal users matching '$DomainSuffix': $($matchingUsers.Count)" -ForegroundColor Green
# Convert mailboxes to shared
$convertedCount = 0
$index = 1
foreach ($user in $matchingUsers) {
$upn = $user.UserPrincipalName
try {
$mailbox = Get-Mailbox -Identity $upn -ErrorAction Stop
if ($mailbox.RecipientTypeDetails -ne "SharedMailbox") {
Set-Mailbox -Identity $upn -Type Shared -ErrorAction Stop
Write-Host "[$index/$($matchingUsers.Count)] Converted to shared: $upn"
$convertedCount++
} else {
Write-Host "[$index/$($matchingUsers.Count)] Already shared: $upn"
}
} catch {
Write-Host "[$index/$($matchingUsers.Count)] Failed for $upn - $($_.Exception.Message)" -ForegroundColor Red
}
$index++
}
Write-Host "`nConversion complete. $convertedCount out of $($matchingUsers.Count) mailboxes converted." -ForegroundColor Cyan
# Disconnect EXO session
Disconnect-ExchangeOnline -Confirm:$false