Office 365 – Update User Profile Property using PowerShell and CSOM

Here is a quick tip on how you can automate the update of Office 365 user profile property using the PowerShell and Client Side Object Model (CSOM).

 

$filePath = "D:\SCRIPTS\OFFICE365"
Set-Location $filePath
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.UserProfiles.dll")

Write-host "Client Library Module(s) Loaded...." -ForegroundColor Green
# Authenticate
$userName = "YOUR USERNAME" 
$siteUrl = "YOUR SITE COLLECTION URL"
$pwd = Read-Host -AsSecureString "Please enter your password"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $pwd)
$peopleManager = new-object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx);
$personProperties = $peopleManager.GetMyProperties();
$ctx.Load($personProperties);
$ctx.ExecuteQuery();
$peopleManager.SetSingleValueProfileProperty($personProperties.AccountName, "AboutMe", "I LOVE OFFICE 365 !!!");
$ctx.ExecuteQuery();