Partilhar via


PowerShell - add a JPG into AD for the user photo

# Update-AD-Picture.ps1
#
# copy a .jpg into a byte array, then push that into
# the "thumbnailPhoto" property of the specified user in AD

function UpDate-AD-Picture {
 param ([STRING] $UserName, [STRING] $JPEGfilePath)

 if ( ($JPEGfilePath) -and (test-path($JPEGfilePath)) ) {
   [BYTE[]] $JPEG = Get-Content $JPEGfilePath -encoding BYTE
 
   $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
   $root = $domain.GetDirectoryEntry()
 
   $search = [System.DirectoryServices.DirectorySearcher] $root
   $search.Filter = "(&(objectclass=user)(objectcategory=person)(samAccountName=$UserName))"
   $result = $search.FindOne()
 
   if ($result -ne $null)  {
     $user = $result.GetDirectoryEntry()
     $user.put("thumbnailPhoto",  $JPEG )
     $user.setinfo()
     Write-output "$($user.DisplayName)  updated"
   }
   else {
     Write-output "'$UserName' was not found in AD."
   }
 }
 else  {
   write-output "The file '$JPEGfilePath' was not found!"
 }

}

Update-AD-Picture "LukeB" "C:\shared\tabitha1.jpg"