{"id":13741,"date":"2011-06-03T00:01:00","date_gmt":"2011-06-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/03\/useless-typingor-playing-with-powershell\/"},"modified":"2011-06-03T00:01:00","modified_gmt":"2011-06-03T00:01:00","slug":"useless-typingor-playing-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/useless-typingor-playing-with-powershell\/","title":{"rendered":"Useless Typing&#8230;or Playing with PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Windows PowerShell MVP, Sean Kearney, talks about a fun Windows PowerShell script he calls &ldquo;useless typing.&rdquo;\nMicrosoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy, as well as today&rsquo;s blog about having some fun with Windows PowerShell. I am not going to be redundant by reposting <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/04\/25\/expert-solution-for-2011-scripting-games-beginner-event-6-parse-the-windows-update-log-for-errors-with-powershell.aspx\">his biography<\/a> each and every day.\nTake it away, Sean&hellip;\nSometimes we code in Windows PowerShell to improve our jobs at work.\nThen there are other times&hellip;well, we just play.\nToday I played. Maybe I over played&hellip;\nSeveral times, I&rsquo;ve seen <b>Start-Demo<\/b> in action, which is a script to display a Windows PowerShell script while executing it. Presenters use it commonly. But I kept seeing it on the screen and thinking to myself, &ldquo;I should make that look like somebody REALLY is typing it&hellip;&rdquo;\nWell, that wasn&rsquo;t difficult. A simple string with a bit of Random, like so:<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$Sentence=&ldquo;It was the best of times, it was the worst of times, because my Cat ate my lunch!&rdquo;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">foreach ($x in 1..$Sentence.Length) {<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Write-host $Sentence[$x-1] &ndash;nonewline<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Start-sleep &ndash;milliseconds (GET-RANDOM 100)<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">}<\/span><\/span><\/span><\/p>\n<p>  <span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">WRITE-HOST<\/span><\/span><\/span><\/p><\/blockquote>\n<p><em>&nbsp;<\/em><i>&nbsp;<\/i>\nGreat! I&rsquo;m done!\nWRONG!\nYou see, just like anything fun, once you start playing, things can get out of control.&nbsp;&nbsp;\nI decided that I wanted NOISE! Some bizarre piece of my head snapped open and said, &ldquo;There MUST be, nay&hellip; there SHALL be, the sound of a typewriter.&rdquo; (Note: the complete <b>Start-Typewriter<\/b> Windows PowerShell script is located in the <a target=\"_blank\" href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/291cc08e-6419-4a0c-9e33-57d2af834ba8\">Scripting Guys Script Repository<\/a>.)\nSo I started digging for some typewriter sounds. I tried cheating by just using <b>Invoke-Item<\/b>, but the problem was that the sounds fell all over each other. So I dug online and found a great article written by Windows PowerShell MVP, Shay Levy, about <a target=\"_blank\" href=\"http:\/\/scriptolog.blogspot.com\/2007\/09\/playing-sounds-in-powershell.html\">Playing Sounds in PowerShell<\/a>. It worked perfectly, except I needed a synchronous play (I wanted to make sure that nothing happened until the noise of the typing went away).\nSo I assigned <b>New-Object <\/b>to<b> $Sound<\/b> per Shay&rsquo;s article, and then I ran <b>Get-Member<\/b> against it, as shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$SOUND = NEW-Object System.Media.SoundPlayer<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$SOUND | GET-MEMBER<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><em>&nbsp;<\/em><i>&nbsp;<\/i>\nI found a method called <strong>Playsync()<\/strong><strong>,<\/strong><strong> <\/strong>which met my needs perfectly. I turned that into a useful function in my script called <b>PlaySound<\/b>. If I pass it the location of the WAV file to play, it will play it.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">function playsound {<\/span><\/span><\/span><span><span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; param($Location)<\/span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp; <\/span><br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $sound=new-object system.media.soundplayer<\/span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $sound.SoundLocation=($Location)<\/span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $sound.playsync()<\/span> <br \/><\/span><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; }<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The next task I decided on was to have it pick that sound randomly. There are a bunch of ways to do it, but this is for fun. I stored them all in a variable called <b>$List<\/b> with <b>Get-ChildItem<\/b> pointing to the folder with the files.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$LIST=GET-CHILDITEM C:PointlessTypingCLACK?.WAV<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>At this point, I could just write a little function called <strong>PlayTypeSound <\/strong>that would (strangely enough) make a typing noise by playing a random one from the available WAV file list.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">function playtypenoise {<\/span><\/span><\/span><span><span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; param($LIST)<\/span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playsound (GET-RANDOM $LIST)<\/span> <br \/><\/span><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; }<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>At some point, my brain walked out the back door and decided that I should have the &ldquo;ever perfect computer&rdquo; make mistakes. So, as the song would go: &ldquo;Conjunction junction, another function&hellip;&rdquo; Every so often, I would have it type two completely random letters, mutter something, and then undo it, as shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">function oops {<\/span><\/span><\/span><span><span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playtypenoise $LIST<\/span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp; <\/span><br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $typo1=[char](97+(GET-RANDOM 26))<\/span> <br \/><\/span><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $typo2=[char](97+(GET-RANDOM 26))<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host $typo1$typo2 &ndash;nonewline<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playsound &#8216;C:Pointlesstypingdoh.wav&#8217;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start-sleep -milliseconds (GET-RANDOM 100)<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $undo=&#8221;`b`b&nbsp; `b`b&#8221;<\/span><\/span><\/span><span><span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><br \/><\/span><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host $undo &ndash;nonewline<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $playtypenoise<\/span><\/span><\/span><span><span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start-sleep -milliseconds (GET-RANDOM 100)<\/span> <br \/><span style=\"font-family: Lucida Sans Typewriter;color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><br \/><\/span><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; }<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><em>&nbsp;<\/em><i>&nbsp;<\/i>\nI even wrote a silly function called <b>FlipCoin<\/b> to decide (based on parameters) if the odds were against our typist friend.<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">function flipcoin {<\/span><\/span><\/span><span><span> <br \/><\/span><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; param($odds=25,$base=50)<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; IF ((GET-RANDOM $BASE) -gt $odds) { return $TRUE }<\/span><\/span><\/span><span><span> <br \/><\/span><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><em>&nbsp;<\/em><i>&nbsp;<\/i>\nThen I went completely mad. I decided that a single line wasn&rsquo;t enough. OH no&hellip;NOOOO! (Muah haha hahah!) I had to have a paragraph! So I tweaked the function a bit and pulled it together. Step through each line in the paragraph, type them suckers, make noise, irritate the neighbors&hellip;<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">foreach ($line in $paragraph) {<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><br \/><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; $length=($line.Length)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; foreach ($x in 0..$length) { <\/span><\/span><span><br \/><\/span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $letter=$line[$x]<\/span> <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host $letter -NoNewline<\/span> <br \/><span>&nbsp;&nbsp;&nbsp; <\/span><br \/><\/span><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (flipcoin 19) { playtypenoise $LIST }<\/span><\/span><\/span><\/span><\/span><span>&nbsp;<\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (flipcoin 47) { oops }<\/span><\/span><span> <br \/><\/span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">}<\/span><\/span><\/span><\/span><span><\/span><\/p>\n<\/blockquote>\n<p><em><\/em>\nSo I played. I REALLY played. If you&rsquo;re curious to hear and see this monstrosity, you can <a target=\"_blank\" href=\"http:\/\/cid-952f95cb5de3f349.office.live.com\/self.aspx\/Powershell\/Pointlesstyping.zip\">download my tiny ZIP file<\/a>, and store the contents of all of this into a little folder called <b>C:PointlessTyping<\/b><em>.<\/em> Run the script <b>Start-Typing.PS1<\/b> to get it all into memory. Then execute either of the following:<\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">START-TYPEWRITER &ndash;paragraph &ldquo;To be or not to be, that is&hellip;. that is&hellip;. oh darn it what was my line?&rdquo;<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Calibri\"><span style=\"color: #000000\">or<\/span><\/span><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">START-TYPEWRITER &ndash;paragraph (GET-CONTENT C:SomeReallyHonkingHugeTextFile.txt)<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><em><\/em>\n&hellip;and, errrr, be prepared to get some funny looks if you run it on a bus&hellip;\nCheers!\nSean, The Energized Tech\nGuest Blogger Week will continue tomorrow when Sean will talk about Windows PowerShell and working with legacy systems. This is a DO NOT DARE MISS series of vital blogs that has been months in the making. A special thank-you to Sean for writing this week&rsquo;s blog posts. Hope you enjoy them.\nI invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, talks about a fun Windows PowerShell script he calls &ldquo;useless typing.&rdquo; Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy, as well as today&rsquo;s [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,2,3,4,154,45],"class_list":["post-13741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Windows PowerShell MVP, Sean Kearney, talks about a fun Windows PowerShell script he calls &ldquo;useless typing.&rdquo; Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy, as well as today&rsquo;s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13741","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13741"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13741\/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=13741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}