SharePoint User Profile (PowerShell)

ported [SharePoint User Profile] to PowerShell for some testing... wanted to save it.

NOTES:
- includes use of Using
- also uses SPServiceContext instead of deprecated ServerContext
Language:
PowerShell
Keywords:
SharePoint, User Profile Service
Code Snippet

#############
##  VARIABLES
#############
$url = "http://SPServer"

##############
##  REFERENCES
##############
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")

#####################

cUsing ( $tSite = New-Object Microsoft.SharePoint.SPSite($url) ) {
    #$sc = [Microsoft.Office.Server.ServerContext]::GetContext($tSite)
    $sc = [Microsoft.SharePoint.SPServiceContext]::GetContext($tSite)
    $upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($sc)

    $accountName = "DOMAIN\\sbrickey"
    $accountName = "i:0#.w|DOMAIN\sbrickey"
    if ($upm.UserExists($accountName))
    {
        $up = $upm.GetUserProfile($accountName)

        # to grab a single property value
        if ($up["WorkEmail"] -ne $null -and
            $up["WorkEmail"].Value -ne $null)
        {   Write-Output("email: " + $up["WorkEmail"].ToString()) }


        # to iterate all properties/values
        foreach ($item in $up.Properties)
        {
            Write-Output("s: " + $item.Name)
            if ($up[$item.Name] -ne $null)
            {   Write-Output("v: " + $up[$item.Name]) }
        }

    }

}


Created 2014-04-21
comments powered by Disqus
Login