Thursday, February 12, 2015

Updating the User Profile Properties value by reading it from csv

Updating the User Profile Properties value by reading it from csv file:

The below script will read the username & update property from the csv file and update the user profile.

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
  Add-PSSnapin Microsoft.SharePoint.PowerShell
}

$csvfile="c:\Temp\UserList.csv"
$mySiteUrl = "http://SiteURL"
$upAttribute = "Location"
$upUserName = "UserName"
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$csvData = Import-Csv $csvfile
foreach ($line in $csvData)
{
       if ($profileManager.UserExists($line.UserName))
       {
              $up = $profileManager.GetUserProfile($line.UserName)
              $up[$upUserName].Value + "|" + $up[$upAttribute].Value | out-file -filepath C:\temp\Before_UserUpdate.txt -append -width 200
              $up[$upAttribute].Value = $line.Location
              $up.Commit()
              $up[$upUserName].Value + "|" + $up[$upAttribute].Value | out-file -filepath C:\temp\After_UserUpdate.txt -append -width 200
             
        }
       else
       {
              $line.username | out-file -filepath C:\temp\UserNotFound.txt -append -width 200

        }
}

$site.Dispose()  

No comments:

Post a Comment