{"id":16131,"date":"2010-12-23T00:01:00","date_gmt":"2010-12-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/12\/23\/a-windows-powershell-carol-ebenezer-script-creates-text-files\/"},"modified":"2010-12-23T00:01:00","modified_gmt":"2010-12-23T00:01:00","slug":"a-windows-powershell-carol-ebenezer-script-creates-text-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-windows-powershell-carol-ebenezer-script-creates-text-files\/","title":{"rendered":"A Windows PowerShell Carol: Ebenezer Script Creates Text Files"},"content":{"rendered":"<p><span style=\"font-size:10.0pt\"><\/span>\n<span style=\"font-size:10.0pt\"><\/span>\n<b><span style=\"font-size:10.0pt\">Summary:<\/span><\/b><span style=\"font-size:10.0pt\"> A Windows PowerShell Carol in which Ebenezer Script learns how to create and to delete text files using PowerShell.<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\"><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hey, Scripting Guy! Will Ebenezer Script ever see how easy it is to use Windows PowerShell to work with text files?<\/span>\n<span style=\"font-size:10.0pt\">&#8212; SK<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\"><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello SK, Microsoft Scripting Guy Ed Wilson here. It is almost the end of the year and we have decided to devote some posts to the holiday season. We even have guest bloggers from around the world to share some holiday spirit. This week we have Sean Kearney. You can learn more about Sean in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/12\/19\/weekend-scripter-holiday-powershell-carol-part-one.aspx\"><span>Monday&rsquo;s blog introduction<\/span><\/a>. Be sure to check out <a href=\"https:\/\/www.youtube.com\/watch?v=v04BXhlPxyY\">Sean&rsquo;s theme song for this week&rsquo;s PowerShell Carol posts<\/a>.<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\">When Ebenezer awoke, his head felt less fuzzy but he was still perplexed. <\/span>\n<span style=\"font-size:10.0pt\">&ldquo;I must lay off the sugar; a giant 8&rdquo; floppy indeed!&rdquo; <\/span>\n<span style=\"font-size:10.0pt\">He tried to shake off the events and get clarity in his head.<\/span>\n<span style=\"font-size:10.0pt\">He went downstairs to fetch a cup of tea from and an old &ldquo;Nutrimatic&rdquo; he&rsquo;d picked up on the internet. He sat down and began arguing with it (as was typical) when he noticed a glowing from within the living room.<\/span>\n<span style=\"font-size:10.0pt\">Half-curious \/ half out of his mind, he wandered off into the living area. In there he encountered a site he never thought to see again. A fifty foot green-bar printout covered in SET statements and billions of quotes. The world&rsquo;s biggest VBScript.<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;E-B-E-N-E-Z-E-R,&rdquo; the giant VBScript boomed out, &ldquo;I AM VBSCRIPT! The Spirit of Scripting Present; touch my code and learn this night!&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">Ebenezer looked up. He gazed over the thousands of lines of code. It was actually his old Administration script from years past. He walked up to touch the lines of code, a distant friend from the past.<\/span>\n<span style=\"font-size:10.0pt\">The giant Script rolled up Ebenezer into its many comments and whisked him away into the air.<\/span>\n<span style=\"font-size:10.0pt\">Moments later, they found themselves in front of a small abode, stuck in a tiny city in Canada.<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Do you know where we are Ebenezer?&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">The Spirit invoked itself through a cscript.exe statement.<\/span>\n<span style=\"font-size:10.0pt\">Ebenezer blinked, &ldquo;Why it appears to be Bob Snipit&rsquo;s place. My assistant.&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Mr. Script, would you like to explain to this poor old VBScript just WHY your assistant lives in Canada? What a tremendous commute THAT must be!&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Cheaper rent and an income tax loophole,&rdquo; mumbled the old mizer as he cursed these all-knowing spirits for revealing what a cheap person he was.<\/span>\n<span style=\"font-size:10.0pt\">Ebenezer looked in the tiny 100mm x 100mm window. <\/span>\n<span style=\"font-size:10.0pt\">&ldquo;What is he doing in there Spirit? He seems oddly happy as poor as he is.&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">The spirit invoked. &ldquo;He is playing a game with his son Tiny Shim. He is coding lines in VBScript while Tiny Shim teaches him how to do it with Windows PowerShell.&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Well Tiny Shim,&rdquo; Bob Snipit smiled away, &ldquo;Mr. Script had me create a new file this morning in VBScript to store his contacts. It was very easy. We just used this code.&rdquo;<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\">S<span style=\"font-family: courier new,courier\">et ManageFiles = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<br \/>Set RemoveFile = ManageFiles.CreateTextFile &ldquo;C:TempFolderNewFile.txt&rdquo;,True<br \/>NewFile.Close<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span>\n<span style=\"font-size:10.0pt\">Tiny Shim looked on. &ldquo;Oh, Father! Silly you. You know as well as I, that&rsquo;s just one line in Windows PowerShell. Remember?&rdquo;<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\">N<span style=\"font-family: courier new,courier\">EW-ITEM -Path C:TempFolder -Name Newfile.txt -ItemType File -Force<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span>\n<span style=\"font-size:10.0pt\">Bob chuckled. &ldquo;Right you are Tiny Shim! I had nearly forgotten! Then later today we had to remove another file in VBScript using this code.&rdquo;<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\">S<span style=\"font-family: courier new,courier\">et ManageFiles = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<br \/>Set RemoveFile = ManageFiles.DeleteFile &ldquo;C:TempFolderOldFile.txt&rdquo;<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span>\n<span style=\"font-size:10.0pt\">Tiny Shim looked up at his Dad giggling. &ldquo;Father you know just as well as I removing that with Windows PowerShell is only:&rdquo;<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\">R<span style=\"font-family: courier new,courier\">EMOVE-ITEM &ndash;Path C:TempFolder &ndash;Name OldFile.txt<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span>\n<span style=\"font-size:10.0pt\">Bob pretended to be dumbfounded slapping his forehead. &ldquo;Oh my! But I thought I could do it like this in Windows PowerShell!&rdquo;<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\">R<span style=\"font-family: courier new,courier\">D C:TempfolderOldfile.txt<\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span>\n<span style=\"font-size:10.0pt\">The young lad looked at his father as if he had just swallowed a bag of alligators. &ldquo;You know as well as I do Father, that&rsquo;s an ALIAS in Windows PowerShell. Aliases allow Windows PowerShell to mimic older commands like in DOS!&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">Bob Snipit looked on with pride at his young son. &ldquo;Such a bright boy!&rdquo; he tapped him on the head with an old coffee stir stick.<\/span>\n<span style=\"font-size:10.0pt\">They went back and forth like this for hours. The father and son were teaching each other Windows PowerShell. Tiny Shim loved to code. He loved to sing. He loved to sing and code. Sometimes he even sang songs that were written in code. <\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Ebenezer,&rdquo; the giant VBScript invoked, &ldquo;see the joy Windows PowerShell brings to Tiny Shim, such wonder and such amazement. He is only eight years old, and yet already a brilliant coder. Someday he will change the world.&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">Mr. Script looked on; a child enjoying scripting. Could he have been wrong about Windows PowerShell?<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Take me away Spirit. I suddenly feel something I haven&rsquo;t felt in a long time. I must be sick.&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;No Ebenezer, it is probably your conscience,&rdquo; the script muttered deep and low as it whisked Ebenezer back to his abode in Redmond.<\/span>\n<i><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/i>\n<i><span style=\"font-size:10.0pt\">Will Ebenezer now look at Windows PowerShell as a scripting solution? Will Tiny Shim become the great coder he should be and change the world? Will Mr. Script seriously consider a cost of living adjustment for Bob Snipit to offset his heavy commute to Canada (or at least let him move into the broom closet)? <\/span><\/i>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\">SK, that is all there is to using Windows PowerShell to work with files. Holiday guest blogger week will continue tomorrow when Sean will bring to us another day of A Windows PowerShell Carol. <\/span>\n<span style=\"font-size:10.0pt\">I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><b><span style=\"font-size:10.0pt\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: A Windows PowerShell Carol in which Ebenezer Script learns how to create and to delete text files using PowerShell. &nbsp; Hey, Scripting Guy! Will Ebenezer Script ever see how easy it is to use Windows PowerShell to work with text files? &#8212; SK &nbsp; Hello SK, Microsoft Scripting Guy Ed Wilson here. It is [&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,3,4,154,14,100],"class_list":["post-16131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-text-files","tag-windows-powershell-ise"],"acf":[],"blog_post_summary":"<p>Summary: A Windows PowerShell Carol in which Ebenezer Script learns how to create and to delete text files using PowerShell. &nbsp; Hey, Scripting Guy! Will Ebenezer Script ever see how easy it is to use Windows PowerShell to work with text files? &#8212; SK &nbsp; Hello SK, Microsoft Scripting Guy Ed Wilson here. It is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16131","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=16131"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16131\/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=16131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}