Repository navigation
Expand file tree
/
Copy pathOffboarding.ps1
More file actions
352 lines (267 loc) · 9.6 KB
/
Copy pathOffboarding.ps1
File metadata and controls
352 lines (267 loc) · 9.6 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<#
.SYNOPSIS
Remove permissions from O365 management of users
.DESCRIPTION
Script that prompts for common actions taken during user offboarding. It will log in to the
user portal on each session and then list available actions to take. Output of before and
after states will be given to terminal for review.
.RESTRICTIONS
This only works fully for O365 managed user accounts. Accounts that are Hybrid/AD-Sync Managed
will encounter errors when attempting to use some functions.
.ISSUES
Microsoft Graph needs permissions granted to be able to access anything and I do not feel comfortable
giving permissions.
.NOTES
Author: Jeremy
Version: 1.0.0
Date: 2026-08-24
History:
1.0.1 - 2026-08-24 - Impliment Remove-Groups function
1.0.0 - 2026-08-17 - Initial script creation.
.PENDINGFUNCTIONS
Remove-Groups
SET-Mailbox-permissions
Remove-License
#>
#Global Variables
$SelectedUser = $null
# Connect Microsoft Graph
Connect-MgGraph `
-ContextScope Process `
-Scopes "User.ReadWrite.All","Group.ReadWrite.All","GroupMember.ReadWrite.All","Directory.Read.All","LicenseAssignment.ReadWrite.All"
#Connect Exchange Online
Connect-ExchangeOnline
function Get-Groups {
if ($null -eq $SelectedUser) {
Write-Host "No user selected." -ForegroundColor Red
return
}
$groups = Get-MgUserMemberOfAsGroup `
-UserID $SelectedUser.Id `
-All
return $groups
}
function Remove-Groups { #currently shows what is considered a group
if ($null -eq $SelectedUser) {
Write-Host "No user selected." -ForegroundColor Red
return
}
#Get Groups
$groups = Get-Groups
if ($null -eq $groups -or $groups.Count -eq 0) {
Write-Host "User is not a member of any groups." -ForegroundColor Yellow
return
}
$groups | Select-Object DisplayName, Id, Mail, MailEnabled, SecurityEnabled |
Format-Table -AutoSize
#Display Groups
Write-Host "`nGroups for $($SelectedUser.UserPrincipalName):" -ForegroundColor Cyan
for ($i = 0; $i -lt $groups.Count; i++) {
Write-Host "[$($i + 1)] $($groups[$i].DisplayName)"
}
# Selection Loop
$selectedGroups = @()
do {
$choice = Read-Host "`nEnter group number, C: Clear, A: Select All and F: Finalize"
if ($choice -eq 'F') {
break
}
if ($choice -eq 'A') {
Write-Host "Function Not Yet Implemented"
}
if ($choice -eq 'C') {
$selectedGroups = @()
}
if ($choice -match '^\d+$') {
$index = [int]$choice - 1
if ($index -ge 0 -and $index -lt $groups.Count) {
$group = $groups[$index]
if ($selectedGroups.Id -notcontains $group.Id) {
$selectedGroups += $group
Write-Host "Selected: $($group.DisplayName)" `
-ForegroundColor Green
}
else {
Write-Host "Group already selected." `
-ForegroundColor Yellow
}
}
else {
Write-Host "Invalid group number." `
-ForegroundColor Red
}
}
else {
Write-Host "Enter group number, C: Clear, A: Select All and F: Finalize" `
-ForegroundColor Red
}
} while ($true)
# Show final selection
Write-Host "`nGroups selected for removal:" -ForegroundColor Cyan
foreach ($group in $selectedGroups) {
Write-Host "Removing $($SelectedUser.UserPrincipalName) from $($group.DisplayName)..."
try {
Remove-MgGroupMemberDirectoryObjectByRef `
-GroupId $group.Id `
-DirectoryObjectId $SelectedUser.Id `
-ErrorAction Stop
Write-Host "Successfully removed." -ForegroundColor Green
}
catch {
Write-Host "FAILED: $($group.DisplayName)" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Yellow
}
}
}
function Disable-UserSignIn {
if ($null -eq $SelectedUser) {
Write-Host "No user selected." -ForegroundColor Red
return
}
Update-MgUser `
-UserId $SelectedUser.Id `
-AccountEnabled:$false
Write-Host "$($SelectedUser.DisplayName) sign-in disabled." -ForegroundColor Green
}
function Select-User {
$searchTerm = Read-Host "Enter user's name or email"
try {
# Looks like an email
if ($searchTerm -match "@") {
$user = Get-MgUser `
-UserId $searchTerm `
-Property Id, DisplayName, UserPrincipalName, Mail
}
else {
# Search by display name
$users = @(Get-MgUser `
-Filter "displayName eq '$searchTerm'" `
-Property Id, DisplayName, UserPrincipalName, Mail)
}
if ($searchTerm -notmatch "@") {
if ($users.Count -eq 0) {
Write-Host "No user found." -ForegroundColor Red
return
}
if ($users.Count -gt 1) {
Write-Host "`nMultiple users found:" -ForegroundColor Yellow
for ($i = 0; $i -lt $users.Count; $i++) {
Write-Host "$($i + 1). $($users[$i].DisplayName) - $($users[$i].UserPrincipalName)"
}
do {
$selection = Read-Host "Select user number"
if ($selection -notmatch '^\d+$') {
Write-Host "Invalid selection. Please enter a number." -ForegroundColor Red
continue
}
$selection = [int]$selection
if ($selection -lt 1 -or $selection -gt $users.Count) {
Write-Host "Invalid selection. Please choose a number between 1 and $($users.Count)." -ForegroundColor Red
continue
}
$validSelection = $true
} while (-not $validSelection)
$user = $users[$selection - 1]
}
else {
$user = $users[0]
}
}
$script:SelectedUser = $user
Write-Host ""
Write-Host "Selected user:" -ForegroundColor Green
Write-Host "Name: $($SelectedUser.DisplayName)"
Write-Host "Email: $($SelectedUser.UserPrincipalName)"
Write-Host ""
}
catch {
Write-Host "Unable to find user: $($_.Exception.Message)" -ForegroundColor Red
}
}
function Convert-ToSharedMailbox {
if ($null -eq $SelectedUser) {
Write-Host "No user selected." -ForegroundColor Red
return
}
Write-Host "Converting $($SelectedUser.DisplayName) to shared mailbox..."
Set-Mailbox -Identity $SelectedUser.UserPrincipalName -Type Shared
Write-Host "Mailbox converted successfully." -ForegroundColor Green
}
#switch option, needs to be at bottom of script
do {
Clear-Host
Write-Host "==============================" -ForegroundColor Cyan
Write-Host " Microsoft 365 Admin" -ForegroundColor Cyan
Write-Host "==============================" -ForegroundColor Cyan
Write-Host ""
Write-Host "1. Disable user sign-in"
Write-Host "2. Remove user from groups"
Write-Host " 2a. Get user grups"
Write-Host "3. Convert mailbox to shared"
Write-Host "4. Change mailbox settings"
Write-Host "5. Remove licenses"
Write-Host "6. Select User"
Write-Host "7. Exit"
Write-Host ""
$choice = Read-Host "Select an option"
switch ($choice) {
"1" {
Write-Host "Running: Disable Sign-in..." -ForegroundColor Yellow
# Your disable user sign-in function here
Disable-UserSignIn
}
"2" {
Write-Host "Running: Remove User From Groups..." -ForegroundColor Yellow
# Your group removal function here
Remove-Groups
}
"2a" {
Write-Host "Running: Getting user groups..." -ForegroundColor Yellow
$groups = Get-Groups
if ($null -eq $groups -or $groups.Count -eq 0) {
Write-Host "User is not a member of any groups." -ForegroundColor Yellow
}
else {
Write-Host "`nGroups for $($SelectedUser.UserPrincipalName):" -ForegroundColor Cyan
Write-Host ""
$groups |
Select-Object DisplayName, Id, Mail, MailEnabled, SecurityEnabled |
Format-Table -AutoSize
}
}
"3" {
Write-Host "Running: Convert Mailbox..." -ForegroundColor Yellow
# Your mailbox conversion function here
Convert-ToSharedMailbox
}
"4" {
Write-Host "Running: Mailbox Settings..." -ForegroundColor Yellow
# Your mail settings function here
Set-MailSettings
}
"5" {
Write-Host "Running: License Removal..." -ForegroundColor Yellow
# Your license function here
Remove-UserLicenses
}
"6" {
Write-Host "Running: Select User..." -ForegroundColor Yellow
# Your Select User function here
Select-User
}
"7" {
Write-Host "Exiting..." -ForegroundColor Green
Disconnect-ExchangeOnline -Confirm:$false
Disconnect-MgGraph
Pause
}
default {
Write-Host "Invalid selection. Please choose 1-7." -ForegroundColor Red
Start-Sleep -Seconds 2
}
}
if ($choice -ne "7") {
Write-Host ""
Read-Host "Press Enter to return to the menu"
}
} while ($choice -ne "7")