Wednesday, January 26, 2011

Get all User Profiles by Using SharePoint PowerShell

Ever need to get SharePoint User Profiles from powershell?  Here’s a quick script to do just that:

$x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$x= [System.Reflection.Assembly]::LoadWithPartialName("microsoft.sharepoint.portal")
$x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
$x= [System.Reflection.Assembly]::LoadWithPartialName("System.Web")

$s = get-spsite
$context = [Microsoft.Office.Server.ServerContext]::GetContext($s[0])
$profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$profileManager.GetEnumerator()

Just save as get-spuserprofiles.ps1, and then you can do things like this:

$p = get-spuserprofiles.ps1

$p = get-spuserprofiles.ps1 | ?{$_[“WorkEmail”] –eq “email@company.com”}