Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

Query remote User Profile with PowerShell and REST API

The below code leverage SharePoint 2013 REST /_api/ to get User Profile detail remotely.   WebClient issues the HTTP GET and saves XML response.   Parsing XML gives full profile detail which can be exported or saved with other functions.   Hope this helps! 

shades_smile

 

NOTE – Remember to replace “sharepoint2013” with target farm DNS and “domain\” with Active Directory domain.

 

function getRemoteUserProfile ($user) {
	# Config
	$url = "http://sharepoint2013/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='domain\$user'"
	$file = "c:\temp\response.html"
	
	# WebClient
	$wc = New-Object System.Net.WebClient
	$wc.UseDefaultCredentials = $true
	$wc.Proxy.Credentials = $wc.Credentials
	$wc.DownloadFile($url, $file)
	# Parse
	$x = Get-Content $file
	$upp = $x.entry.content.properties.UserProfileProperties
	$costcenter = ($upp.element |? {$_.Key -eq "costcenter"}).Value
	$division = ($upp.element |? {$_.Key -eq "division"}).Value
	$department = ($upp.element |? {$_.Key -eq "department"}).Value
	# Display
	$obj = New-Object –Type PSObject –Prop @{'User'=$user;'costcenter'=$costcenter;'division'=$division;'department'=$department}
	return $obj
}
# Main
getRemoteUserProfile "sptest1"
getRemoteUserProfile "sptest2"
getRemoteUserProfile "sptest3"

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲