PowerShell Function to export a user's thumbnail photo from AD

The following function is used to export a user's thumbnail photo from Active Directory (AD) to a JPG file on disk.
This function uses the ActiveDirectory CMDlets included in the Active Directory Module for PowerShell, this is part of the Remote Server Administrative Tools (RSAT) pack available from Microsoft for client OSs.

Note that once RSAT has been installed, you will need to enable the Active Directory Module for PowerShell feature via Add and Remove Programs.

function Get-UserPhoto(){  
    param(
        [string]$Username,
        [string]$Path = "C:\Temp\AD_Photos"
    )

    Import-Module ActiveDirectory
    $user = Get-ADUser $Username -properties thumbnailPhoto
    if(!(test-path $Path)){mkdir $Path}
    $user.thumbnailPhoto | Set-Content ("$Path\$($User.SamAccountName).jpg") -Encoding byte
}