Tuesday, 22 September 2015

Get user profile properties in sharepoint with powershell

# Add SharePoint cmdlets reference

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

function enumerateWebParts($Url) {
    

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

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles") 

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

# Enter your SharePoint site URL here... 

$site = new-object Microsoft.SharePoint.SPSite($Url); 

  

$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site); 

  

$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext) 

  

$AllProfiles = $ProfileManager.GetEnumerator() 

  

write-host "UserName ,AccountName , Manager ,PictureURL"

  

foreach($profile in $AllProfiles) 



  

$DisplayName = $profile.DisplayName 

  

$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value 

  

$Picturevalue = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::PictureURL].Value 
$Manager = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::Manager].Value
$Employee_ID = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::UserName].Value
$Employee_Name = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::PreferredName].Value
$Employee_Email = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::WorkEmail].Value
$Employee_Department = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::Department].Value
#$Manager_Email = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::Department].Value
if ([System.String]::IsNullOrEmpty($Picturevalue))
{

write-host $Employee_ID","$Employee_Name","$Employee_Email","$Manager","$Employee_Department

$profile| select-object @{Expression={$Employee_ID};Label="Employee ID"},@{Expression={$Employee_Name};Label="Employee Name"}, @{Expression={$Employee_Email};Label="Employee Email"}, @{Expression={$Manager};Label="Manager Name"},@{Expression={$Employee_Department};Label="Department"}
}





  

$site.Dispose()

}


$webAppUrl = "https://test.com"
$csvpath = Read-Host "Enter the csv file path to create for ex: c:\test.csv"

$row = enumerateWebParts($webAppUrl)
$row | Export-Csv $csvpath
       

No comments:

Post a Comment