[void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint") | out-null [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server.Search") | out-null [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null # Function: Get-SPProfileManager # Description: Return a UserProfileManager object which is used for accessing MOSS User Profiles function Get-SPProfileManager() { PARAM ( [string]$PortalURL = $( throw "You must provide a Site Collection Url e.g. 'http://moss/'") ) END { # Need to get a PortalContext object # as we do not have a HttpContext we need to source one the hard way $site=new-object Microsoft.SharePoint.SPSite($PortalURL) $servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site) $site.Dispose() # clean up # Return the UserProfileManager new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($servercontext) } } # ****************************************************************************************************** # * This script requires an input file which includes the usernames of all to be deleted user profiles * # ****************************************************************************************************** # * * # * Format of usernames is: \ * # * One username per line * # * * # ****************************************************************************************************** $siteUrl = "" $pm=get-spprofilemanager $siteUrl get-content userprofiles.txt | Foreach-Object { "Check for user $_" if ($pm.UserExists($_)) { $pm.RemoveUserProfile($_); "--- User profile removed!" } if ($pm.UserExists($_)) { "--- User profile exists!" } else { "--- User profile does not exist!" } }