Issue with Set-UserPhoto command


I have a PowerShell script that runs on a schedule to set all of our 150 users profile photos within Office365/Microsoft365.

Here is the code I am using…

$username = “<username>”
$password = “<password>” | ConvertTo-SecureString -asPlainText -Force
$Usercredential = New-Object System.Management.Automation.PSCredential($username,$password)
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Usercredential -Authentication Basic -AllowRedirection
Import-PSSession $ExSession -AllowClobber

Set-UserPhoto -Identity “” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user001.jpg”)) -Confirm:$falsecls
Set-UserPhoto -Identity “” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user002.jpg”)) -Confirm:$falsecls
Set-UserPhoto -Identity “” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user150.jpg”)) -Confirm:$falsecls

The script used to work fine, however recently after about every 10 photos that are changed a prompt appears asking for the password to be re-entered:

Creating a new session for implicit remoting of “Set-UserPhoto” command…

I haven’t changed anything recently so I’m wondering why this is now prompting me?

It’s really annoying as I cannot now run this as a scheduled task.

Can anyone recommend a fix?

Best wishes,


Hello Matt,

Try to play with -SessionOption parameter. Create PSsession object using New-PSSessionOption cmdlet and tweak Idletimeout value.


Hope that helps.