{"id":2466,"date":"2013-12-06T09:00:00","date_gmt":"2013-12-06T09:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/06\/solving-office-holiday-gift-giving-conundrums-with-powershell\/"},"modified":"2013-12-06T09:00:00","modified_gmt":"2013-12-06T09:00:00","slug":"solving-office-holiday-gift-giving-conundrums-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/solving-office-holiday-gift-giving-conundrums-with-powershell\/","title":{"rendered":"Solving Office Holiday Gift-Giving Conundrums with PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about a project to assign secret pals at work. He ropes one of the managers into the project.<\/p>\n<p>During my one-on-one last week&nbsp;with my manager, Adina, she mentioned doing something to help people who work from remote locations feel more like part of the team. Because I work remotely, I was all ears. She had a great idea: Assign random secret pals. You have to find out all you can about the secret pal, and then send them something that is unique to them&mdash;their interests, hobbies, or tastes. Of course, the present is something small, which also makes it a bit of a challenge. Although, maybe not. I mean, anyone who reads the Hey, Scripting Guy! Blog for very long knows that I enjoy different kinds of teas, and I am especially partial to Anzac biscuits&mdash;neither of which need be especially expensive.<\/p>\n<p>So it is that kind of thing. And of course, I was in. Then Adina came up with a real kicker, &ldquo;I wonder if you could write a Windows PowerShell script to do that?&rdquo;<\/p>\n<p>So I opened my big mouth and said, &ldquo;Of course, I could write it. But with Windows PowerShell, the task would be so easy, that even a manager could write it.&rdquo;<\/p>\n<p>Now I had Adina&rsquo;s attention. We discussed it a bit, and decided that Michel would be perfect for the task because as he has nearly no experience with Windows PowerShell. I offered, of course, to help with pointers. But before I got too involved, Michel had already solved the task at hand. I now turn you over to Michel LaFantano, a senior managing editor on our team, and I will let him tell you about his experience. Take it away Michel&hellip;<\/p>\n<p>Our team at Microsoft includes many remote workers&mdash;some who work in non-Redmond Microsoft offices, and some who work from their homes around the country. This helps make for a diverse team, and it also lends a certain &ldquo;open 24 hours&rdquo; feeling to our group because there is pretty much always someone awake and online.<\/p>\n<p>But it also means that remote workers rarely get the chance to hang-out in person, and we usually miss the morale events held on the Redmond campus. To help remedy that a bit this year, our director decided to hold a remote worker gift exchange. But how do we pick names from the fish bowl when people are dispersed across ten time zones?<\/p>\n<p>Well, we figured a little Windows PowerShell script might help solve the issue. Being a resourceful kind of guy, I figured I would take a look in the Script Repository (or scriptorium, as I like to call it) and see if I could find something similar to leverage in creating our own &ldquo;remote-gift-giver-partner-picker&rdquo; script.<\/p>\n<p>What I found was beyond all expectations, because I found a script that one of our awesome users had written for this exact purpose. Our scripter, who goes by the handle &ldquo;CathersalCathersalCathersal,&rdquo; wrote it for his family&rsquo;s Secret Santa event, and it is called the &ldquo;Virtual Hat.&rdquo; Take a look:<\/p>\n<h2>Virtual Hat Windows PowerShell script<\/h2>\n<p style=\"padding-left: 30px\"># This script will randomly draw a name out of a virtual &#8220;hat.&#8221;<\/p>\n<p style=\"padding-left: 30px\">#<\/p>\n<p style=\"padding-left: 30px\"># This script selects two names from an array in a random fashion.<\/p>\n<p style=\"padding-left: 30px\"># Because the people will exchange gifts, it is important that no<\/p>\n<p style=\"padding-left: 30px\"># person is assigned to themselves.<\/p>\n<p style=\"padding-left: 30px\">#<\/p>\n<p style=\"padding-left: 30px\"># Instructions: Just change the names below to the names you want and run it.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$NameArray = New-Object System.Collections.ArrayList<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Syed&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Kim&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Sam&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Hazem&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Pilar&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Terry&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Amy&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Pamela&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&ldquo;Julie&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;David&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Robert&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Shai&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Ann&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Mason&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$NameArray.Add(&#8220;Sharon&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$numberOfNames = $namearray.count<\/p>\n<p style=\"padding-left: 30px\">$ArrayCopy = $namearray.clone()<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">for($i=0; $i -lt $numberOfNames; $i++)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $NameToRemove = get-random -input $NameArray<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; While ( $NameToRemove -eq $ArrayCopy[$i])&nbsp; # make sure people don&#8217;t pick themselves.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $NameToRemove = get-random -input $NameArray<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Write-host $ArrayCopy[$i]&#8221;picks $NameToRemove&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $NameArray.Remove($NameToRemove)&nbsp; # Remove the name so it doesn&#8217;t get picked again.<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>When I run the script, the output in the following image appears in the Windows PowerShell ISE:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6443.HSG-12-6-13-B-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6443.HSG-12-6-13-B-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Simple, yet elegant, don&rsquo;t you think? And all we had to do to use it was add the names of our remote team members and we were good to go.<\/p>\n<p>We all know that Windows PowerShell is an awesome tool in datacenters and at home, but who knew that it could also save the holidays? Issue solved. Back to you, Ed&hellip;<\/p>\n<h2>Turn an array into a hash table<\/h2>\n<p>While Michel was busy finding a script to use, I had also begun to tackle the issue. I figured it would be pretty simple because the <strong>Get-Random<\/strong> cmdlet is a powerful tool. In fact, we use the <strong>Get-Random<\/strong> cmdlet when handing out prizes at <a href=\"http:\/\/powershellsaturday.com\/\" target=\"_blank\">PowerShell Saturday<\/a> and at <a href=\"http:\/\/powershellgroup.org\/\" target=\"_blank\">Windows PowerShell User Group meetings<\/a>. So I am familiar with this tool.<\/p>\n<p>If all I wanted to do was randomize a list of names, that is two lines of code (it could even be one if I do not store the array of names in its own variable). But I wanted a two-column output. At first I thought I could use <strong>Format-Wide<\/strong> and specify two columns. But after messing around with it for about a minute, I saw that for some reason, <strong>Format-Wide<\/strong> does not like to convert an array into two-column output.<\/p>\n<p>So I decided to convert my array of names into a hash table. Here is the script I wrote to do this:<\/p>\n<p style=\"padding-left: 30px\"># ArrayNamesToHashTable.ps1<\/p>\n<p style=\"padding-left: 30px\"># ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\"># Secret Pals blog posting<\/p>\n<p style=\"padding-left: 30px\"># HSG-12-6-13-b<\/p>\n<p style=\"padding-left: 30px\"># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">$names = &#8216;Syed&#8217;, &#8216;Kim&#8217;, &#8216;Sam&#8217;, &#8216;Hazem&#8217;, &#8216;Pilar&#8217;, &#8216;Terry&#8217;, &#8216;Amy&#8217;, &#8216;Greg&#8217;,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8216;Pamela&#8217;, &#8216;Julie&#8217;, &#8216;David&#8217;, &#8216;Robert&#8217;, &#8216;Shai&#8217;, &#8216;Ann&#8217;, &#8216;Mason&#8217;, &#8216;Sharon&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$rndNames = Get-Random -InputObject $names -Count ($names.count)<\/p>\n<p style=\"padding-left: 30px\">$hash = @{} ; $i = 0<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$hash.Add($rndNames[$i],$rndNames[($i+1)])<\/p>\n<p style=\"padding-left: 30px\">&nbsp;if($i -le $names.count){$i+=2}}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;While ($i -le $names.count-1)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$hash<\/p>\n<p>When I run it, I get a nice two-column output, as shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0878.HSG-12-6-13-B-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0878.HSG-12-6-13-B-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>So in the end, it took me more time than it did for a manager to &ldquo;write a script.&rdquo; All told, I spent about an hour messing around with this script. But I imagine that I had more fun writing my script than Michel did. After all, I used the Windows PowerShell ISE and he used Bing.<\/p>\n<p>You can find the script in the Script Repository: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/3ad5a37d-2c3a-42ae-b341-40140a763e67\" target=\"_blank\">Randomly draw names out of a virtual &#8220;hat.&#8221;<\/a><\/p>\n<p>Join me tomorrow when I will have more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me 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 email to me 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, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about a project to assign secret pals at work. He ropes one of the managers into the project. During my one-on-one last week&nbsp;with my manager, Adina, she mentioned doing something to help people who work from remote locations feel more like part of the team. Because I work [&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":[470,51,3,4,45],"class_list":["post-2466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about a project to assign secret pals at work. He ropes one of the managers into the project. During my one-on-one last week&nbsp;with my manager, Adina, she mentioned doing something to help people who work from remote locations feel more like part of the team. Because I work [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2466","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=2466"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2466\/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=2466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}