How to remove remove several Office 365 users in bulk!

This script allows to remove several Office 365 users in bulk. The script defines a function that reads a csv file with the users to be deleted. The CSV file defines a single column that is used for the function to determine the users to be deleted. Remove-MsolUser and Get-Msol

 
 
 
 
 
4.5 Star
(2)
3,443 times
Add to favorites
Office 365
10/18/2018
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • I am getting the below output, can you tell me what i am missing
    3 Posts | Last post February 21, 2019
    • Loading  for processing...
      Deleting the Office 365 users ...
      System.Management.Automation.RuntimeException: You cannot call a method on a null-valued expression.
         at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
         at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
         at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
         at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
      
      THANKS
    • Are you sure the file required for the removal processs is well configured?
    • How did you fix this problem? I'm trying to run the script and I'm getting the same error.
      
      Thanks,
      Nelson. 
  • Getting Error when running the script
    1 Posts | Last post December 12, 2018
    • I am getting the following error when i run he script
      
      Remove-MsolUser : User Not Found in the Microsoft Online directory Deleted Users container.  User: c876a9d8-0232-4f1c-a1fa-ef6372d58474.
      At line:35 char:66
      + ... ame $fila.$sColumnName | Remove-MsolUser -Force -RemoveFromRecycleBin
      +                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : OperationStopped: (:) [Remove-MsolUser], MicrosoftOnlineException
          + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.RemoveUser
      
      do the accounts need to have been previously deleted before we run this? I am looking to delete them and removing them from the the tennant
      
      The accounts still exist in Azure and 0365 after the error above as well.
  • THANK YOU !! The script works .
    2 Posts | Last post October 18, 2018
    • Hello JC.
      
      The script seems to run well. I remove the comment on the Remove-MsolUser cmdlet on line 35.  However, after running the script against 200+ users, I still see them on the office portal and I'm able to login as some of them. 
      
      Question:
      
      Does this take a while to kick in and complete the operation?
      
      Please advise.
      
      Thanks.
      
      CL
    • Hi CL,
      It should remove them as soon as the remove-msoluser action is done...are the removed Users being placed in the recycle bin? Do you by chance have your local AD connected to Office 365 with Azure AD Connect?
  • This is great, thanks
    2 Posts | Last post June 23, 2018
    • Thanks for the script, really helped me out! Only -RemoveFromRecycleBin didn't work for me, so I left that out.
    • Glad you have find the script useful!
  • modify script for removing External Contacts
    1 Posts | Last post March 22, 2018
    • I'm not a programmer but this script seems like it would work best for my needs with, probably, one change. I'd like to pull from a list of External Contacts to be deleted in Exchange. I'm thinking the cmdlet Remove-MailContact is needed here. Can you either produce an alternate script or explain how to modify this one to do this task?
  • Nothing happens – PowerShell ISE just closes
    1 Posts | Last post December 05, 2017
    • This is the first time that I've used 'Windows PowerShell ISE' and nothing happens when I try to follow your instructions!
      
      I prepared a test file 'UsersToDeleteFile.csv', with a single column labelled 'UserPrincipalName', containing only one user's details.
      
      After opening 'Windows PowerShell ISE' I pasted the following script and entered my account details to sign-in to my 'Office 365 for Business' account:
      
      $UserCredential = Get-Credential
      Connect-MsolService -Credential $UserCredential
      
      I then copied and pasted your script without the comment mark for 'Remove-MsolUser'.  When a ran it, I was again prompted for my account sign-in details and after I re-entered them, the 'Windows PowerShell ISE' window closed, without displaying any message and the user in the test CSV file hadn't been deleted.
      
      What am I doing wrong and how does your script know where to find the 'UsersToDeleteFile.csv' file on my computer?
      
      Thank-you and best regards
  • Does it take a while to actually delete the users?
    5 Posts | Last post September 30, 2017
    • I was able to successfully run the script and got several indications from the script that the users were deleted, but that was over a hour ago and if I go to my Offie 365 Admin portal, those users are still there...
      
      Does it take a while for these accounts to actually delete after running the script?
    • Never mind, script works great! I just didn't realize you had the Remove-MsolUser commented out!
      
      Actually a smart idea
    • Glad it helped!!!
    • I ran this script, the results were "All the users have been deleted. The processs is completed."
      It seems the script was working fine, but once i back to portal > activate users, these accounts are still exiting here. Would you mind to share the experience how you resolve it?
    • As commented by Ricky, you need to uncomment the Remove-MSolUser sentence in the script
  • 'Connect-MsolService' is not recognized as the name of a cmdlet
    3 Posts | Last post June 20, 2017
    • Hi,
      
      I am receiving the below error when running the script. Can you advise please? Thank you
      
      PS C:\Users\ahadmin\Desktop> C:\Users\ahadmin\Desktop\PS_RemoveOffice365Users.ps1
      Connect-MsolService : The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
      the spelling of the name, or if a path was included, verify that the path is correct and try again.
      At C:\Users\ahadmin\Desktop\PS_RemoveOffice365Users.ps1:13 char:1
      + Connect-MsolService -Credential $msolcred
      + ~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : ObjectNotFound: (Connect-MsolService:String) [], CommandNotFoundException
          + FullyQualifiedErrorId : CommandNotFoundException
       
      Loading  for processing...
      Deleting the Office 365 users ...
      System.Management.Automation.RuntimeException: You cannot call a method on a null-valued expression.
         at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
         at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
         at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
         at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
    • Did you install the PowerShell Module for Office 365 on your machine?
    • I am getting this error. I dont know where to place the csv file or how to name it.
      
      Test-Path : Caracteres no válidos en la ruta de acceso.
      En C:\Users\dt\Downloads\PS_RemoveOffice365Users.ps1: 22 Carácter: 25
      +         $bFileExists = (Test-Path $sInputFile -PathType Leaf)
      +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (C:\Users\dt\Dow...DeleteFile>.csv:String) [Test-Path], ArgumentExcepti
         on
          + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.TestPathCommand
      
      C:\Users\dt\Downloads\<UsersToDeleteFile>.csv file not found. Stopping the import process!
  • Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
    3 Posts | Last post January 31, 2017
    • PS C:\scripts> .\PS_RemoveOffice365Users.ps1
      cmdlet Get-Credential at command pipeline position 1
      Supply values for the following parameters:
      Loading  for processing...
      Deleting the Office 365 users ...
      Deleting user bstocker@
      Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
      At C:\scripts\PS_RemoveOffice365Users.ps1:31 char:45
      +             Get-MsolUser -UserPrincipalName $fila.$sNombreColumna | Remove-MsolU ...
      +                                             ~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidData: (:) [Get-MsolUser], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automation.GetUser
       
      Deleting user cgustafson@
      Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
      At C:\scripts\PS_RemoveOffice365Users.ps1:31 char:45
      +             Get-MsolUser -UserPrincipalName $fila.$sNombreColumna | Remove-MsolU ...
      +                                             ~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidData: (:) [Get-MsolUser], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automation.GetUser
       
      Deleting user cmortenson@fusd1.org
      Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
      At C:\scripts\PS_RemoveOffice365Users.ps1:31 char:45
      +             Get-MsolUser -UserPrincipalName $fila.$sNombreColumna | Remove-MsolU ...
      +                                             ~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidData: (:) [Get-MsolUser], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automation.GetUser
       
      What am I missing?  thanks
    • worked for me after changing line 31 to this
      
      Get-MsolUser -UserPrincipalName $fila.$sColumnName | Remove-MsolUser -Force
    • Yeap, sorry for the bug..it was my fault! Now the script is corrected
  • Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
    2 Posts | Last post January 31, 2017
    • Hi
      
      I am trying to delete a bunch of user account using this script. However i get this error when executing. 
      
      Deleting the Office 365 users ...
      Deleting user MyPlace.Test1@centricaplc.onmicrosoft.com
      Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
      At M:\Services\Office_365\PowerShell Scripts\RemoveOffice365Users.ps1:33 char:45
      +             Get-MsolUser -UserPrincipalName $fila.$sNombreColumna | Remove-MsolU ...
      +                                             ~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidData: (:) [Get-MsolUser], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automation.GetUser
      
      Can any one help please?
    • Hello Sunny,
      The script had two bugs I have just corrected...once of the errors is the one you have reported. Basically, the column name to be read from the CSV file was wrong.
1 - 10 of 17 Items