How can I delete SharePoint Online Profile Pictures programatically?

Copper Contributor

Does anyone know if there's a way to delete SharePoint Online Profile Pictures programmatically?

 

For On Premise SharePoint, I'm managing this via this PowerShell Scripts:

 

# Convert string $objectSid to byte $byteSID

$sid = New-Object System.Security.Principal.SecurityIdentifier ($objectSid)
$byteSID = New-Object 'byte[]' $sid.BinaryLength
$sid.GetBinaryForm($byteSID, 0)

 

$site=new-object Microsoft.SharePoint.SPSite("http://onprem.sharepoint.local")

$serviceContext = Get-SPServiceContext $site

$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)

 

$Userprofile = $upm.GetUserProfile($byteSID)

$Userprofile["PictureURL"].value=$null
$Userprofile.Commit()

 

These users does not have Exchange Online Mailboxes, so I cannot use Set-UserPhoto

I managed to upload photos using via this method, but I cannot remove the pictures once uploaded:

https://github.com/SharePoint/PnP/tree/master/Samples/Core.ProfilePictureUploader

 

1 Reply

You can try this pnp powershell script to remove picture URL

 

Connect-PnPOnline -Url https://<tenantname>-admin.sharepoint.com
set-PnPUserProfileProperty -Account alexw@<tenantname>.onmicrosoft.com -PropertyName "PictureURL" -Value ""