Can I get the Powershell script to block the users sign-in and remove the licenses and convert the usermailbox to sharedMailbox and assign the sharedmailbox to someone else by using script in M365 by using input file?

Vinod Survase 4,726 Reputation points
2024-02-25T09:47:29.08+00:00

Can I get the Powershell script to block the users sign-in and remove the licenses and convert the usermailbox to sharedMailbox and assign the sharedmailbox to someone else by using script in M365 by using input file?

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
4,348 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,382 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,323 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Miguel Gonçalves | AVANADE 961 Reputation points
    2024-06-14T22:59:13.5633333+00:00

    Hi Vinod Survase,

    Please check if make sense for your case.

    A sample PowerShell script that you can use as a starting point; (Path_to_your_CSV_file.csv).

    CSV file with a list of users to be processed. The CSV file should have a column named ‘UserPrincipalName’ for the user to be converted, and ‘AssignTo’ for the user to whom the mailbox will be assigned.

    # If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    # Import the required module
    Import-Module MSOnline
    # Connect to Microsoft 365
    $credential = Get-Credential
    Connect-MsolService -Credential $credential
    # Import user data from CSV file
    $users = Import-Csv -Path "Path_to_your_CSV_file.csv"
    foreach ($user in $users) {
        # Block the user
        Set-MsolUser -UserPrincipalName $user.UserPrincipalName -BlockCredential $true
        # Remove all licenses
        $licenses = (Get-MsolUser -UserPrincipalName $user.UserPrincipalName).Licenses
        foreach ($license in $licenses) {
            Remove-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -LicenseOptions $license
        }
        # Convert to shared mailbox and assign to another user
        Set-Mailbox -Identity $user.UserPrincipalName -Type Shared
        Add-MailboxPermission -Identity $user.UserPrincipalName -User $user.AssignTo -AccessRights FullAccess -InheritanceType All
    }
    
    0 comments No comments