{"id":50683,"date":"2010-04-11T00:01:00","date_gmt":"2010-04-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/04\/11\/hey-scripting-guy-weekend-scripter-the-scripting-wife-redirects-output-and-creates-a-text-file\/"},"modified":"2010-04-11T00:01:00","modified_gmt":"2010-04-11T00:01:00","slug":"hey-scripting-guy-weekend-scripter-the-scripting-wife-redirects-output-and-creates-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-the-scripting-wife-redirects-output-and-creates-a-text-file\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: The Scripting Wife Redirects Output and Creates a Text File"},"content":{"rendered":"<p><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/p>\n<p><p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I love it when it rains. It sings of cleansing, freshness, new birth, and endless possibilities. Especially when it is a gentle spring rain like we are enjoying right now. It reminds me of when the Scripting Wife and I first went to Maui, Hawaii. We were staying near the beach, and every afternoon it would rain for about a half hour. We could just about set our watches by the rain showers. When the rain stopped, the sun came back out, and if we were lucky we were greeted with a rainbow. The following photo is one I took of one of those afternoon rainbows. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"A photo Ed took of a rainbow on Maui\" alt=\"A photo Ed took of a rainbow on Maui\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0411\/wes-04-11-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">I enjoy sitting out on the front porch and watching the rain, depending of course on which way the wind is blowing. I do not enjoy sitting on the front porch and getting drenched, but I do enjoy the cool breeze and the sweet smell of spring rain. I was just about to lean back in the swing, close my eyes, and visualize tropical rainbows, when the Scripting Wife materialized in the swing seat next to me. She had her laptop with her and a look of concern upon her countenance, so I knew my daydreaming was rapidly drawing to a close. <\/p>\n<p class=\"MsoNormal\">&ldquo;Isn&rsquo;t it a lovely afternoon shower? If you close your eyes, and let your mind wander, it will remind you of Maui,&rdquo; I offered. <\/p>\n<p class=\"MsoNormal\">&ldquo;Can it, Script Monkey. You are not going to weasel out of teaching me. You see my laptop, and you know I am having a hard time learning Windows PowerShell. I want you to explain text files to me. I see they are on the review guide for the 2010 Scripting Games,&rdquo; she said with a smile. <\/p>\n<p class=\"MsoNormal\">&ldquo;It is Microsoft Scripting Guy, not Microsoft Script Monkey,&rdquo; I said by way of clarification.<\/p>\n<p class=\"MsoNormal\">&ldquo;Script Monkey, Script Monkey, Script Monkey,&rdquo; she said mockingly. <\/p>\n<p class=\"MsoNormal\">&ldquo;Now, do you really think that is a good way to ask for help?&rdquo; I asked with exaggerated seriousness.<\/p>\n<p class=\"MsoNormal\">&ldquo;Yes, I do.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;How could you possibly come out here, call me &lsquo;Script Monkey&rsquo; and expect me to help you?&rdquo; I asked.<\/p>\n<p class=\"MsoNormal\">&ldquo;Because I&rsquo;m cute,&rdquo; she parried. <\/p>\n<p class=\"MsoNormal\">She had a good point. <\/p>\n<p class=\"MsoNormal\">&ldquo;Text files in Windows PowerShell are really easy,&rdquo; I began. &ldquo;You can redirect output and create a text file at the same time.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;There you go again with geek speak,&rdquo; she interrupted. &ldquo;What do you mean by redirect output?&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Redirection is taking the output from a command, and sending it somewhere else. For example, when you type <b><span>Get-Process<\/span><\/b>, the cmdlet displays a lot of information on the screen. If you would like to write that information to a text file, you can use the redirection operator, which is the greater than symbol, to cause the information to go to the file instead of being displayed on the screen. The cool thing is that the greater than symbol looks like it is funneling the output to the file. The command to write the process information to a text file would be <b><span>Get-Process &gt; c:fsoprocess.txt<\/span><\/b>,&rdquo; I said.<\/p>\n<p class=\"MsoNormal\">&ldquo;Okay. Let me try it. I learn better by doing than by listening,&rdquo; she said.<\/p>\n<p class=\"MsoNormal\">After a few seconds of typing, I heard her exclaim, &ldquo;Dude, I hate this stuff!&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;What is wrong, my dear?&rdquo; I asked with sincerity.<\/p>\n<p class=\"MsoNormal\">&ldquo;Look. I got one of those bogus error messages,&rdquo; she said, showing me what you see in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of what Scripting Wife thought was bogus error message\" alt=\"Image of what Scripting Wife thought was bogus error message\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0411\/wes-04-11-10-02.jpg\" width=\"600\" height=\"241\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">&ldquo;Uh, my dear, that is not a bogus error message. It says that it cannot find part of the path c:fsoprocess.txt. Now which part of the file path do you think it is unable to find?&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;The folder,&rdquo; she offered tentatively.<\/p>\n<p class=\"MsoNormal\">&ldquo;That makes sense. Let&rsquo;s see if the c:fso folder exists.&rdquo;<\/p>\n<p class=\"MsoNormal\">The Scripting Wife reaches for the mouse.<\/p>\n<p class=\"MsoNormal\">&ldquo;Stop! Let go of that mouse. We can use Windows PowerShell to test for the existence of the c:fso folder,&rdquo; I said. &ldquo;What do you think the name of a cmdlet would be that will test for the existence of the folder?&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Test-Folder,&rdquo; she stated.<\/p>\n<p class=\"MsoNormal\">&ldquo;Close. It is Test-Path. You see a folder is also part of a path,&rdquo; I explained. &ldquo;Now type <b><span>Test-Path c:fso <\/span><\/b>and press ENTER.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;It says false,&rdquo; she said.<\/p>\n<p class=\"MsoNormal\">&ldquo;That means the folder does not exist. So let&rsquo;s create it. That sounds like fun, huh?&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;This could very well be the next Disney ride. That&rsquo;s how much fun it sounds like.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Okay, then. Type <b><span>md c:fso<\/span><\/b> and press ENTER. You see <b>md<\/b> stands for make directory. It is actually an alias for a diff\nerent command. You remember aliases? We talked about them a couple weeks ago.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;No. You didn&rsquo;t. You told me to read about them, and you pointed me to a Hey, Scripting Guy! article, but it really did not make too much sense to me,&rdquo; she complained.<\/p>\n<p class=\"MsoNormal\">&ldquo;Well, alrighty then. An alias is a shortcut name or a nickname for a longer cmdlet. Take for example the <b>Get-Process<\/b> cmdlet; it has an alias of <b>GPS<\/b>, which I remember as standing for <b>G<\/b> for <b>Get<\/b>, <b>P<\/b> for <b>Proce<\/b>, and <b>S<\/b> for <b>s<\/b>. <b>Get-WmiObject<\/b> has an alias of <b>GWMI<\/b> which I remember as&hellip;&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;I get it. <b>G<\/b> for <b>Get<\/b> and <b>WMI<\/b> for <b>WMIObject<\/b>. Is it case sensitive?&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;No. You can type <b>GWMI<\/b> or <b>gwmi<\/b> or <b>gWmi<\/b> or&hellip;&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;I don&rsquo;t have all day. So you said that <b>md<\/b> is an alias and it stands for make directory. Correct?&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Well, yes and no. I sort of lied to you. There is an old DOS command <b>md<\/b> that everyone called <b>make directory<\/b>. <b>MD<\/b> is actually an alias for a function called <b>mkdir<\/b> that is used to create folders,&rdquo; I said.<\/p>\n<p class=\"MsoNormal\">&ldquo;Oh, I see.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Whenever you want to see if something is an alias, use the <b>Get-Alias<\/b> cmdlet. For example, to see if <b>md<\/b> is an alias, you type <b>Get-Alias md<\/b>,&rdquo; I added. &ldquo;To see all of the aliases, type <b>Get-Alias <\/b>by itself.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Cool. Can I show you what I have typed so far,&rdquo; she asked. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Get-Alias output\" alt=\"Image of Get-Alias output\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0411\/wes-04-11-10-03.jpg\" width=\"600\" height=\"443\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">&ldquo;Okay. Good. Let&rsquo;s get back to creating a text file. Now that we have created the c:fso folder, we are ready to write process information to it. Press the up arrow a couple of times until you retrieve the <b>Get-Process &gt; C:fsoprocess.txt<\/b> command and then press ENTER.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;It did not do anything,&rdquo; she said.<\/p>\n<p class=\"MsoNormal\">&ldquo;Did it give you an error?&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;No.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Then it probably worked. You can use the <b>Test-Path<\/b> cmdlet to see if the file exists. Type <b><span>Test-Path c:fsoprocess.txt<\/span><\/b><span> <\/span>and press ENTER.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;It says true,&rdquo; she said.<\/p>\n<p class=\"MsoNormal\">&ldquo;Then truly it exists,&rdquo; I said. &ldquo;Let&rsquo;s open it up in Notepad and look at it. Type <b><span>notepad<\/span> <\/b><b><span>c:fsoprocess.txt<\/span><\/b> and press ENTER.&rdquo;<\/p>\n<p class=\"MsoNormal\">&ldquo;Groovy. It worked,&rdquo; she said as she turned her monitor around so I could see.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of text file created by Scripting Wife\" alt=\"Image of text file created by Scripting Wife\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0411\/wes-04-11-10-04.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">&ldquo;Sweet. Why don&rsquo;t you go play around with that some more, and we will talk about this in more detail later,&rdquo; I suggested.<\/p>\n<p class=\"MsoNormal\">&ldquo;Good idea. Be careful and don&rsquo;t get all wet and come tracking back in the house,&rdquo; she said as she closed the door to the house behind her and faded from view. <\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/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 class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. I love it when it rains. It sings of cleansing, freshness, new birth, and endless possibilities. Especially when it is a gentle spring rain like we are enjoying right now. It reminds me of when the Scripting Wife and I first went to Maui, Hawaii. We were staying [&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":[51,3,4,74,14,61,45],"class_list":["post-50683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-scripting-wife","tag-text-files","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. I love it when it rains. It sings of cleansing, freshness, new birth, and endless possibilities. Especially when it is a gentle spring rain like we are enjoying right now. It reminds me of when the Scripting Wife and I first went to Maui, Hawaii. We were staying [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50683","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=50683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50683\/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=50683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}