Setting the user tile image in Windows 7 and Server 2008 R2

I’ve blogged in the past about adding Thumbnails to Active Directory but I could not believe that I hadn’t thought about how these do not translate into the login tile in Windows 7 and Server 2008 R2. Surely it’s just logical for this to be easy.

I looked around the Internet and found source code for you to create your own .exe file and many complicated answers, but with a little help I’ve got the basics down to a very simple script indeed.

Yes, PowerShell again. User Tiles are a 6.1 feature and installation of PowerShell as standard is a 6.1 feature. For those of you who interested and don’t know, Windows 7 and Windows Server 2008 R2 are “version 6.1”. I think I called it “Longhorn, fixed” when it first came out.

This little script takes a username, which has already been used to log onto the local PC, and an image file. The undocumented function at ordinal 262 does all of the hard work. It resizes the image and does the magic.

As I’ve worked on it, I’ve found variations around the internet of the bits I’ve done so far. Such as this which helped me with the Single Threaded Appartment issue.


# This script msut be run in STA mode to avoid casting errors
# powershell.exe -STA
param ( [string] $User, [string] $ImagePath )

# Create the call to the User Tile API
[string] $SourceCode = @”
[DllImport(“shell32.dll”, EntryPoint = “#262”, CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void SetUserTile(string username, int whatever, string picpath);
“@
$type = Add-Type -Namespace iammarkharrison -Name Tiles -MemberDefinition $SourceCode -PassThru

#Set the User Tile
[iammarkharrison.Tiles]::SetTile($User,$ImagePath)

That’s a good start and it’s certainly the shortest script that does this job that I’ve seen, but having blogged on how to upload thumbnailPhoto in Active Directory, I want to download the thumbnail from AD and use that.

More to follow on that subject. Well, there would have been if someone else had not done it already. It’s really simple to convert the above script.


# Create the call to the User Tile API
[string] $SourceCode = @"
[DllImport("shell32.dll", EntryPoint = "#262", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void SetUserTile(string username, int whatever, string picpath);
"@
$type = Add-Type -Namespace iammarkharrison -Name Tiles -MemberDefinition $SourceCode -PassThru

if ($host.Runspace.ApartmentState -ne “STA”) {
# Relaunch if not in STA mode
powershell.exe -noprofile -sta “$($myInvocation.MyCommand.Path)”
}
else {
#Set the User Tile
$photo = ([ADSISEARCHER]”samaccountname=$($env:username)”).findone().properties.thumbnailphoto
$photo | set-content “$($env:temp)\$($env:userdomain)$($env:username).jpg” -Encoding byte
[iammarkharrison.Tiles]::SetUserTile(“$($env:userdomain)\$($env:username)”,0,”$($env:temp)\$($env:userdomain)$($env
:username).jpg”)
Exit
}

This should now be runnable in a logon script. The one thing I want to address is how small the thumbnails are in AD.

Advertisements

2 thoughts on “Setting the user tile image in Windows 7 and Server 2008 R2

  1. Pingback: User Tile population from AD thumbnailPhoto Windows 8.1

  2. I am not a programmer nor have I messed with powershell. I need to prep different WIN7 systems and I have 2 users that each need a different user acct pic. I need to do this for each new system type. My 2 users are service(full admin) and suser(standard user) and the pics are located in C:\syssetup\pics\service.jpg and C:\syssetup\pics\suser.jpg. Can you tell me what I would need to change in the script and how to include it in a batch file. Thanks.

Leave a Reply

Please log in using one of these methods to post your comment:

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