3

I have a powershell script which is fixing some corrupted userprofiles. But the results are also groups. I would like to skip the groups and fix only the users. How can I check if the spuser object is a user or group in Powershell?

$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://portal.myCompany.net") 

foreach ($site in $webApp.Sites) 
{ 
     foreach($webSite in $site.AllWebs)
     {
        $siteUrl = $site.Url
        $webUrl = $webSite.Url
        write-host $siteUrl
        write-host $webUrl

        $allCoruptedUsers = Get-SPUser -Limit All –Web  $webUrl | Where-Object {$_.DisplayName –like "myDomain*"} 

        # get all users from project portal
        foreach ($user in $allCoruptedUsers)
        {    
            #TODO: CHECK IF THIS $USER OBJECT IS A USER AND NOT A GROUP
            write-host " - fix displayname" $user.LoginName -ForeGroundColor Green
            Set-SPUser -Identity $user -Web $webUrl -SyncFromAD   
        }

        write-host ""
    }
}

1 Answer 1

1

Check the type of the object:

if($user.GetType().FullName.Equals("Microsoft.SharePoint.SPUser"))
{
}
2
  • I still get errors: Set-SPUser : Cannot get the full name or e-mail address of user domainname\adGroup
    – Ola
    Oct 1, 2014 at 13:17
  • Look like you have imported to sharepoint AD groups as users. So the easiest way is to put Set-SPUser into try/catch block. try{Set-SPUser -Identity $user -Web $webUrl -SyncFromAD}catch{write-host 'well, it's not a user:' $user.LoginName} Oct 1, 2014 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.