Remove Inactive users with powershell script from all SharePoint Sites

Chandra Mounika Katta 26 Reputation points
2022-10-31T10:25:08.37+00:00

Hi,

We have a lot of inactive users in SharePoint sites. Inactive users are still remaining in SharePoint sites. They must be removed from all SharePoint sites.

The users will inactive once they left the Organization.

Inactive users deleting process should be Automatic and scheduled.

All suggested links and scripts are not working.

I tried below suggested links.

  1. https://social.msdn.microsoft.com/Forums/en-US/044f3d9e-8202-4746-8c3b-d5974f54ddf5/delete-inactive-users-from-sharepoint?forum=sharepointgeneralprevious
  2. https://social.msdn.microsoft.com/Forums/en-US/c0f713fd-7627-4f94-81b6-e34410bfcc28/automatically-deleting-inactive-sites?forum=sharepointgenerallegacy
  3. https://www.sharepointdiary.com/2013/07/delete-users-from-site-collection-using-powershell.html
  4. https://www.sharepointdiary.com/2013/07/find-and-delete-orphaned-users-in-sharepoint-using-powershell.html
  5. https://learn.microsoft.com/en-us/powershell/module/sharepoint-server/remove-spuser?view=sharepoint-server-ps

Please provide the suitable powershell script to Detect and remove multiple inactive users from SharePoint sites.

Thanks in advance
Mounika

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,273 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,865 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 22,961 Reputation points Microsoft Vendor
    2022-11-01T10:01:23.237+00:00

    Hi @Chandra Mounika Katta ,
    per your problem description, I cannot confirm the version of SharePoint you are using. May I know the version of SharePoint you are using?
    According to my research, deleting Inactive users from all SharePoint sites is essentially deleting Inactive users from AD. I found some articles for your reference:
    https://devblogs.microsoft.com/scripting/use-powershell-to-find-and-remove-inactive-active-directory-users/
    https://learn.microsoft.com/en-us/services-hub/health/remediation-steps-ad/regularly-check-for-and-remove-inactive-user-accounts-in-active-directory
    https://cloudinfrastructureservices.co.uk/find-inactive-users-in-active-directory-using-powershell-script/
    https://spandcrm.com/2017/12/12/sharepoint-2013-how-to-automatically-remove-disabled-active-directory-users/#:~:text=Access%20'Manage%20Service%20Applications'.,'User%20Profile%20Service%20application'.&text=Locate%20the%20'My%20Site%20Cleanup,users%20being%20removed%20from%20SharePoint.
    https://www.lepide.com/how-to/manage-inactive-accounts-in-active-directory.html

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.

    Regards,
    *
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Yanli Jiang - MSFT 22,961 Reputation points Microsoft Vendor
    2022-11-04T09:25:27.97+00:00

    Hi @Chandra Mounika Katta ,
    According to your description, your requirements can be implemented in two steps:
    1, First find the Inactive user in AD and export the csv file.
    257157-11041.png
    257147-11042.png
    257125-11043.png
    For more details, please refer to:
    https://cloudinfrastructureservices.co.uk/find-inactive-users-in-active-directory-using-powershell-script

    2, Then delete the users in the csv file from the site collection.

    #import csv file  
    $users = Import-Csv -Path c:\temp\InactiveUser.csv  
    #Iterate through the csv  
    foreach($user in $userNames)  
    {   
       #Call the function  
       Delete-UserFromAllSites "https://sharepoint.crescent.com" $user $true  
    }  
    

    For more information, please refer to:
    https://www.sharepointdiary.com/2013/07/delete-users-from-site-collection-using-powershell.html

    You can also directly refer to this article:
    https://adamsorenson.com/sharepoint-powershell-script-to-remove-users-from-site-collection/

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.
    *
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.