Search code examples
powershellactive-directory

How to replace all Active Directory account photos?


I have a powershell script to replace a single photo:

Import-Module ActiveDirectory
$photo = [byte[]](Get-Content C:\scripts\foto\XXX.jpg -Encoding byte)
Set-ADUser XXX -Replace @{thumbnailPhoto=$photo}

I have a text file with the usernames of the accounts in:

ROB ALAN COY GREGOR

I want to automate replacing all photo in Active Directory. How would I do this?


Solution

  • I could't comprehend Ashigore's probably right one-liner. Here it is broken down line by line into a foreach loop.

    $users = get-adUser
    foreach ($user in $users){
       $photoPath = "C:\scripts\foto\$($_.SamAccountName).jpg"
       if (Test-Path $photoPath ) {
           $photo = Get-Content "C:\scripts\foto\$($user.SamAccountName).jpg" -Encoding byte
           Set-ADUser $user -Replace @{thumbnailPhoto=$photo}
       }
    }