Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #PowerShell Script – Delete Inactive User Profiles – SharePoint 2010/2013
  2.  
  3. #The scripts is distributet "as-is." Use it on your own risk. The author give no warranties, guarantees or conditions.
  4.  
  5. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
  6. Add-PSSnapin "Microsoft.SharePoint.PowerShell"
  7. }
  8.  
  9. $site = Get-SPSite "<site url>"
  10. $ctx = Get-SPServiceContext $site
  11. $pm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ctx)
  12.  
  13. $ProfileDB = Get-SPDatabase | ? { $_.Type -eq "Microsoft.Office.Server.Administration.ProfileDatabase"}
  14.  
  15. $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  16. $SqlConnection.ConnectionString = $ProfileDB.DatabaseConnectionString
  17. $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  18. $SqlCmd.CommandText = "select NTName,RecordId from UserProfile_Full where bDeleted=1″
  19. $SqlCmd.Connection = $SqlConnection
  20. $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  21. $SqlAdapter.SelectCommand = $SqlCmd
  22. $DataSet = New-Object System.Data.DataSet
  23. $SqlAdapter.Fill($DataSet)
  24. $SqlConnection.Close()
  25.  
  26. Write-host "Total Count: " $DataSet.Tables[0].Rows.Count
  27. Write-Host "Following Inactive Accounts will be deleted !"
  28.  
  29. foreach($user in $DataSet.Tables[0].Rows)
  30. {
  31. write-host "Planning to delete :" $user["NTName"] -ForegroundColor Green
  32. $profile = $pm.GetProfile($user["RecordId"])
  33. #To enable delete operation remove comment out for below line
  34. #$pm.RemoveProfile($profile)
  35. #write-host $user["NTName"] is deleted!!! -ForegroundColor Red
  36. }
  37. write-host "Operation Completed !"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement