Get/Set Active Directory Photos by using PowerShell


Hello everyone. Today a colleague asked me if I know of a simple way to get user pictures from Active Directory. Of course I do and I will show you how you can get and set pictures in Active Directory by using PowerShell.

Important: The following scripts require Remote Server Administration Tools to be installed on your computer if you do not run them on a Domain Controller.

To get pictures from a user in Active Directory simply run the script below and provide a UserName and a Path (For example: C:\example.jpg) where the picture should be stored:

Param([parameter(Mandatory=$true)][alias("User")]$UserName, [parameter(Mandatory=$true)][alias("Picture")]$PicturePath)

Import-Module ActiveDirectory

$user = Get-ADUser $UserName -Properties thumbnailPhoto
$user.thumbnailPhoto | Set-Content $PicturePath -Encoding byte

To update a users photo simply run the script below and provide a UserName and Path of the new picture:

Param([parameter(Mandatory=$true)][alias("User")]$UserName, [parameter(Mandatory=$true)][alias("Picture")]$PicturePath)

Import-Module ActiveDirectory

$photo = [byte[]](Get-Content $PicturePath -Encoding byte)
Set-ADUser $UserName -Replace @{thumbnailPhoto=$photo}

That´s it. You can get/set pictures in Active Directory as simple as this.

As always you can download my scripts from here.

Sources:

http://241931348f64b1d1.wordpress.com/2011/05/12/how-to-put-and-retrive-active-directory-photo-attribute/

 

Advertisements
Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: