Set User Profile Picture in Microsoft SharePoint Server 2010

Introduction

This script changes user profile picture in Microsoft SharePoint Server 2010. It finds user profile pictures from specified picture library. After finding the pictures, it uses User Profile Manager to change PictureUrl property for each user who has user profile.

Scenarios

Under some circumstances, administrators may manage user pictures manually without enabling User Profile Synchronization Service. It indicates that the pictures will not be imported from external systems. They need to find a way to help HR specialist to import user profile picture for each user.

Script

This script contains one advanced function, Set-OSCSPUserPicture. You can use this script in the following way:

1. Open SharePoint 2010 Management Shell.

2. Run Import-Module cmdlet to import this module file.
Import-Module filepath\scriptname.psm1

Note Before running this script, please make sure your account has sufficient permission to connect to user profile service application and manage user profiles. Please refer to following documentations for delegating administration to a service application administrator.

Assign administration of a User Profile service application (SharePoint Server 2010)
Assign administration of user profiles (SharePoint Server 2010)

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
if ($spPicItem.Count -eq 1) { 
    if ($pscmdlet.ShouldProcess($userDisplayName)) { 
        $absoluteUrl = [System.String]::Concat($spPicLib.ParentWeb.Url,"/",$spPicItem[0].Url) 
        $absoluteUrl = [Microsoft.SharePoint.Utilities.SPEncode]::UrlEncodeAsUrl($absoluteUrl) 
        $userProfile["PictureUrl"].Value = $absoluteUrl$userProfile.Commit() 
    } 
} else { 
    $warningMsg = $Messages.CannotFindPic 
    $warningMsg = $warningMsg-$userDisplayName$pscmdlet.WriteWarning($warningMsg) 
    if ($UseDefaultPicture) { 
        $warningMsg = $Messages.UseDefaultPicture 
        $warningMsg = $warningMsg-$userDisplayName$pscmdlet.WriteWarning($warningMsg)                         
        $userProfile["PictureUrl"].Value = $null$userProfile.Commit() 
    } 
}

Examples

Example 01:  How to display help about the Set-OSCSPUserPicture function.
To display help about this function, please run this command: 
Get-Help Set-OSCSPUserPicture -Full

Example 02: How to confirm the users whose picture will be changed.
To confirm the users whose picture will be changed, please run this command: 
Set-OSCSPUserPicture -MySiteHostLocation "http://server_name/my" -UserProfileServiceApplicationName "User Profile Service Application" -UserPictureLibrary "http://server_name/hr/User%20Pictures/" -Whatif

Example 03: How to change user pictures if these pictures can be found in the picture library.
To change user pictures if these pictures can be found in the picture library, please run this command: 
Set-OSCSPUserPicture -MySiteHostLocation "http://server_name/my" -UserProfileServiceApplicationName "User Profile Service Application" -UserPictureLibrary "http://server_name/hr/User%20Pictures/" -Verbose

Note Set-OSCSPUserPicture finds user pictures by using DisplayName by default. More technically, it uses following CAML query to find user pictures. If the query returns nothing or more than one list items, you will see a warning message that indicates Set-OSCSPUserPicture cannot find a specific picture for the user.

"<Where><BeginsWith><FieldRef Name='NameOrTitle' /><Value Type='Computed'>$userDisplayName</Value></BeginsWith></Where>"

Example 04: How to change user pictures if these pictures can be found in the picture library. And the naming convention of user picture file is SamAccountName.jpg.
To change user pictures if these pictures can be found in the picture library, please run this command: 
Set-OSCSPUserPicture -MySiteHostLocation "http://server_name/my" -UserProfileServiceApplicationName "User Profile Service Application" -UserPictureLibrary "http://server_name/hr/User%20Pictures/" -PictureNameFormat SamAccountName -Verbose

Example 05: How to change user pictures by using default picture if these pictures can be found in the picture library.
To change user pictures by using default picture if these pictures can be found in the picture library, please run this command: 
Set-OSCSPUserPicture -MySiteHostLocation "http://server_name/my" -UserProfileServiceApplicationName "User Profile Service Application" -UserPictureLibrary "http://server_name/hr/User%20Pictures/" -UseDefaultPicture -Verbose

Additional Resources

Technical Resources:

Windows PowerShell Advanced Function
Assign administration of a User Profile service application (SharePoint Server 2010)
Assign administration of user profiles (SharePoint Server 2010)
Forum Threads:
PowerShell set PictureUrl - Use default unless another picture is present