-
Notifications
You must be signed in to change notification settings - Fork 0
/
o365-user-disable.ps1
52 lines (40 loc) · 1.46 KB
/
o365-user-disable.ps1
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
<#
File: o365-user-disable.ps1
Date: 2021JAN15
Author: William Blair
Contact: [email protected]
Note: run all files from c:\temp
#>
<#
This script will do the following:
- change user email address
- remove all groups
- add group called 'disabled'
- remove all licenses
#>
# Get credentials and authenticate
$365Logon = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-Credential $365Logon -Authentication Basic -AllowRedirection
Import-PSSession $Session
# Clears the screen
Clear-Host
# Prompt for user
$upn = Read-Host -Prompt 'Enter the email address of the employee you wish to change'
# string called disabled which we will use later concactenate with email address
$string_disabled = 'disabled.'
# This is going to be the new disabled email account name
$upn_disabled = $string_disabled + $upn
# Change the email account name
Connect-MsolService
Set-MsolUserPrincipalName -UserPrincipalName $upn -NewUserPrincipalName $upn_disabled
#Let's update $upn now
$upn = $upn_disabled
#Remove user from all assigned groups
& '.\Remove_User_All_Groups.ps1' -Identity $upn -IncludeAADSecurityGroups -IncludeOffice365Groups
#Remove all license from user acount
(get-MsolUser -UserPrincipalName $upn).licenses.AccountSkuId |
foreach{
Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $_
}