You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function will remove graph users from an excel worksheet. It is part of a module I'm creating. I'll update when module is publicly published. Excelsheet name is hard coded for "Users" and the column names need to be UserName, FirstName, LastName, Title, Department, StreetAddress, City, State, PostalCode, Country, PhoneNumber, MobilePhone, UsageLocation, and License.
<#.SYNOPSISRemoves a user from Microsoft 365 based on the provided Excel data..DESCRIPTIONThe Remove-CT365User function connects to the Microsoft Graph, reads user data from the provided Excel file, and attempts to remove each user listed in the file from Microsoft 365..PARAMETERFilePathSpecifies the full path to the Excel file that contains the user data. This parameter is mandatory..PARAMETERDomainSpecifies the domain that will be concatenated with the UserPrincipalName to form a valid email address. This parameter is mandatory..EXAMPLERemove-CT365User -FilePath "C:\Path\to\file.xlsx" -Domain "example.com"This command attempts to remove the users listed in the "file.xlsx" Excel file from the "example.com" domain..INPUTSSystem.String. You can pipe a string that contains the file path and domain to Remove-CT365User..OUTPUTSSystem.String. Outputs a message for each attempted user removal, indicating success or failure..NOTESThis function requires the Microsoft.Graph.Users and ImportExcel modules. Make sure to install them using Install-Module before running this function..LINKhttps://docs.microsoft.com/en-us/powershell/module/microsoft.graph.users/?view=graph-powershell-1.0.LINKhttps://www.powershellgallery.com/packages/ImportExcel#>functionRemove-CT365User {
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]$FilePath,
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]$domain
)
if (!(Test-Path$FilePath)) {
Write-Warning"File $FilePath does not exist. Please check the file path and try again."return
}
# Import Required ModulesImport-Module Microsoft.Graph.Users
Import-Module ImportExcel
Import-Module PSFramework
# Connect to Microsoft GraphConnect-MgGraph-Scopes "User.ReadWrite.All"# Check if the Excel file exists before importingif (Test-Path$FilePath) {
# Import user data from Excel file$userData=Import-Excel-Path $FilePath-WorksheetName Users
foreach ($userin$userData) {
$UserPrincipalName=$user.MailNickname$GivenName=$user.FirstName$Surname=$user.LastName$MailNickname=$user.MailNickname$JobTitle=$user.Title$Department=$user.Department$NewUserParams=@{
UserPrincipalName="$userPrincipalName@$domain"GivenName=$GivenNameSurname=$SurnameDisplayName="$GivenName$Surname"MailNickname=$MailNicknameJobTitle=$JobTitleDepartment=$Department
}
$userToRemove=Get-MgUser|Where-Object {$_.DisplayName-eq$NewUserParams.DisplayName}
Write-PSFMessage-Level Output -Message "Attemping to remove User $($NewUserParams.DisplayName)"-Target $NewUserParams.DisplayName# Validate if the user existsif ($userToRemove) {
Remove-MgUser-UserId $userToRemove.id# Check the user's existence$removedUser=Get-MgUser|Where-Object {$_.DisplayName-eq$NewUserParams.DisplayName}
# Confirm that the user was removedif (-not$removedUser) {
Write-PSFMessage-Level Output -Message "User $($NewUserParams.DisplayName) has been successfully removed."-Target $NewUserParams.DisplayName
} else {
Write-PSFMessage-Level Warning -Message "Failed to remove user $($NewUserParams.DisplayName)."-Target $NewUserParams.DisplayName
}
} else {
Write-PSFMessage-Level Warning -Message "User $($NewUserParams.DisplayName) does not exist."-Target $NewUserParams.DisplayName
}
}
}
Disconnect-MgGraph
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This function will remove graph users from an excel worksheet. It is part of a module I'm creating. I'll update when module is publicly published. Excelsheet name is hard coded for "Users" and the column names need to be UserName, FirstName, LastName, Title, Department, StreetAddress, City, State, PostalCode, Country, PhoneNumber, MobilePhone, UsageLocation, and License.
Beta Was this translation helpful? Give feedback.
All reactions