{"id":841,"date":"2014-08-09T00:01:00","date_gmt":"2014-08-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/08\/09\/weekend-scripter-exporting-and-importing-photos-in-active-directory\/"},"modified":"2014-08-09T00:01:00","modified_gmt":"2014-08-09T00:01:00","slug":"weekend-scripter-exporting-and-importing-photos-in-active-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-exporting-and-importing-photos-in-active-directory\/","title":{"rendered":"Weekend Scripter: Exporting and Importing Photos in Active Directory"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Use Windows PowerShell to import and export photos in Active Directory.<\/span><\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here. I was playing about with Active Directory this week. I wanted to be able to get photos to automatically populate on my workstations running Windows&nbsp;7.<\/p>\n<p>By doing some online reading, I found that you can store this in the <b>ThumbnailPhoto<\/b> attribute within Active Directory.<\/p>\n<p>In truth this that is not a hidden feature. If you&#039;re running Exchange Server&nbsp;2013, Exchange Server&nbsp;2010, or Lync, there are already cmdlets to do this.<\/p>\n<p>But I wanted to play. I actually wanted two options on my plate. (What can I say&#8230;I&#039;m greedy!)<\/p>\n<p>I wanted the ability to import and export the pictures to and from Active Directory. Yes, I know. I&#039;m just mad about the power from Windows PowerShell!<\/p>\n<p>So what do I need?<\/p>\n<ul>\n<li>A picture in .jpg format and no larger than 100&nbsp;KB in size.<\/li>\n<li>Microsoft Active Directory<\/li>\n<li>Windows PowerShell<\/li>\n<li>100 pounds of baked beans and a panda<\/li>\n<\/ul>\n<p>OK, fine. I was completely lying about the last bullet. I wanted to see if you were reading.<\/p>\n<p>So we&#039;re going to work with the Microsoft Active Directory cmdlets, although the process for the Quest cmdlets is very similar.<\/p>\n<p>First off, I&#039;m going to start with a simple .jpg file. I swiped one from my Twitter account:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/SeanPic.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/SeanPic.jpg\" alt=\"Photo of Sean Kearney\" title=\"Photo of Sean Kearney\" \/><\/a><\/p>\n<p>I used Microsoft Paint to drop the resolution to 52 x 52 pixels while I was playing. So it&#039;s really small, but you&#039;re supposed to be able to upload a photo of 100&nbsp;KB in size.<\/p>\n<p>So the file is sitting on my hard drive in a little folder called C:\\Photos, and the file is named Sean.jpg.<\/p>\n<p>To bring in the file, I have to use a little .NET to read the file:<\/p>\n<p style=\"margin-left:30px\">[SYSTEM.IO.FILE]::ReadAllBytes()<\/p>\n<p>We&#039;re going to read the JPG file as a binary stream because that&#039;s what the Active Directory <b>ThumbnailPhoto<\/b> attribute needs. So let&#039;s add the attribute to this &quot;Sean guy:&quot;<\/p>\n<p style=\"margin-left:30px\">$Picture=[System.IO.File]::ReadAllBytes(&#039;C:\\Photos\\sean.jpg&#039;)<\/p>\n<p style=\"margin-left:30px\">SET-ADUser SeanK &ndash;add @{thumbnailphoto=$Picture}<\/p>\n<p>If you had this information in Active Directory and you need to export it (for any reason at all), we can do that too.<\/p>\n<p>First we grab the <b>User<\/b> object from Active Directory:<\/p>\n<p style=\"margin-left:30px\">$User=GET-ADUser SeanK &ndash;properties thumbnailphoto<\/p>\n<p>Then we export the information with a little more .NET magic:<\/p>\n<p style=\"margin-left:30px\">$Filename=&#039;C:\\Photos\\Export.jpg&#039;<\/p>\n<p style=\"margin-left:30px\">[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)<\/p>\n<p>Pretty cool, eh?<\/p>\n<p>We could even go so far as to export all the photos in Active Directory to the file system:<\/p>\n<p style=\"margin-left:30px\">$list=GET-ADuser &ndash;filter * -properties thumbnailphoto<\/p>\n<p style=\"margin-left:30px\">Foreach ($User in $list)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">$Directory=&#039;C:\\Photos\\&#039;<\/p>\n<p style=\"margin-left:30px\">If ($User.thumbnailphoto)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;$Filename=$Directory+$User.samaccountname+&#039;.jpg&#039;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;}<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>If you aren&#039;t running Exchange Server&nbsp;2013, Exchange Server&nbsp;2010, or Lync, and you would like to play with this idea further, there&#039;s an excellent article and solution written by a fellow in the community named <a href=\"https:\/\/plus.google.com\/+JimmyMathewP?prsrc=5\" target=\"_blank\">Jimmy Mathew<\/a>. He created a really cool solution to allow you to set the photo by using the stored picture in Active Directory: <a href=\"http:\/\/www.parackattu.com\/2014\/04\/windows-8-windows-81-set-account.html\" target=\"_blank\">Windows&nbsp;8\/Windows 8.1: Set Account Picture from Active Directory<\/a>.<\/p>\n<p>Now let&#039;s get going! Start populating and truly personalizing your Active Directory environment!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send an email to the Scripting Guys at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, remember, the Power of Shell is in You.<\/p>\n<p><b>Sean Kearney, Windows PowerShell MVP, Honorary Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to import and export photos in Active Directory. Honorary Scripting Guy, Sean Kearney, is here. I was playing about with Active Directory this week. I wanted to be able to get photos to automatically populate on my workstations running Windows&nbsp;7. By doing some online reading, I found that you can store [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,154,61,45],"class_list":["post-841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Windows PowerShell to import and export photos in Active Directory. Honorary Scripting Guy, Sean Kearney, is here. I was playing about with Active Directory this week. I wanted to be able to get photos to automatically populate on my workstations running Windows&nbsp;7. By doing some online reading, I found that you can store [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=841"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}