{"id":53003,"date":"2009-07-07T03:01:00","date_gmt":"2009-07-07T03:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/07\/07\/hey-scripting-guy-how-can-i-both-save-information-in-a-file-and-display-it-on-the-screen\/"},"modified":"2009-07-07T03:01:00","modified_gmt":"2009-07-07T03:01:00","slug":"hey-scripting-guy-how-can-i-both-save-information-in-a-file-and-display-it-on-the-screen","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-both-save-information-in-a-file-and-display-it-on-the-screen\/","title":{"rendered":"Hey, Scripting Guy! How Can I Both Save Information in a File and Display It on the Screen?"},"content":{"rendered":"<h2><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> Hey, Scripting Guy! <span><span><font size=\"2\">You know the old commercial that goes, &#8220;Sometimes you feel like a nut, sometimes you don&#8217;t&#8221;? Well, sometimes I want to save information from a script to a file, and sometimes I want to see it displayed to the screen. And at other times, I want both. In other words, I want my candy bar, and I want to eat it, too.<\/font><\/span><\/span><\/p>\n<p><font color=\"#333333\">&#8211; NR<\/font><\/p>\n<p><font color=\"#333333\"><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font> <\/p>\n<p>Hi NR,<\/p>\n<p><span><span>I never did really get the expression, &#8220;I want my cake and I want to eat it too.&#8221; I mean, what good is cake if you cannot eat it? Or for that matter, who would want a cake that they only used for decoration? To me it doesn&#8217;t make sense. I think there is art, and there is food. Food is to be consumed, and art is to be appreciated but not eaten. When I go to the <a href=\"http:\/\/www.guggenheim.org\/\"><font face=\"Segoe\">Guggenheim<\/font><\/a> in New York City, I go there to nourish my soul, not my belly. When I go to the Sydney Opera House, seen in the following picture I took during my last trip to Australia, I can actually do both because there is an excellent restaurant under the front sail.<\/span><\/span> <\/p>\n<p><img decoding=\"async\" title=\"Image of the Sydney Opera House\" alt=\"Image of the Sydney Opera House\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/july\/hey0707\/hsg-07-07-09-01.jpg\" width=\"467\" height=\"244\"><\/p>\n<p><p class=\"MsoNormal\">If you want to display information on the screen, you run the command and by default it will send the information to the console. This is seen here:<\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_bios<\/p>\n<p><br>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">If you want to store the information in a text file, you can use the redirection arrow as seen here:<\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt; Get-WmiObject -Class win32_bios &gt;c:fsobios.txt<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">The problem is there is neither a confirmation message that the command completed successfully, nor an idea of what is in the text file. There is a cmdlet you can use&mdash;the <b>Out-File<\/b> cmdlet. But once again, as seen here, there is no feedback from the command:<\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_bios | Out-File -FilePath C:fsobios.txt<br>PS C:&gt;<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">One thing you could do is to use the <b>Get-Content<\/b> cmdlet to inspect the contents of the file to ensure it has the information you require. The thing to keep in mind is that you are not piping the information from the <b>Out-File<\/b> cmdlet to the <b>Get-Content<\/b> cmdlet. The semicolon is used to indicate that you are beginning a new command. The semicolon is the equivalent of typing the command on a new line in a script. This is seen here:<\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_bios | Out-File -FilePath C:fsobios.txt ; <br>Get-Content -Path C:fsobios.txt<\/p>\n<p><br>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">Because you have already seen that the redirection arrow is the same as using the <b>Out-File<\/b> cmdlet, for your purposes here you can revise the command to use the redirection arrows. You can also shorten the command a bit by using the alias <b>cat<\/b> instead of the longer <b>Get-Content<\/b> cmdlet name. This command is seen here:<\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_bios &gt; C:fsobios.txt ; cat C:fsobios.txt<\/p>\n<p>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">By using an alias for the <b>Get-WmiObject<\/b> cmdlet and also leaving out the class parameter name, you can create a short command: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; gwmi win32_bios &gt; C:fsobios.txt ; cat C:fsobios.txt<\/p>\n<p><br>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">Now you have a shorter command that you can use to feed the content from the command to a text file for storage, and then display the information on the console. Though this is a workable solution, it would be easier if there was a cmdlet that would essentially do this for us. As it turns out, there is a cmdlet that will split the output from a cmdlet and direct it to both the screen and to a file. The cmdlet is called the <b>Tee-Object<\/b>. Most of the time, you will split the output from your command line to a file and to the console. To do this, you will use the <b>filepath<\/b> parameter and specify the full path to the file. As seen here, the <b>Tee-Object<\/b> cmdlet supports a number of additional switches and parameters. <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">Tee-Object [-FilePath] &lt;String&gt; [-InputObject &lt;PSObject&gt;] [-Verbose] [-Debug] <br>[-ErrorAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-OutVariable <br>&lt;String&gt;] [-OutBuffer &lt;Int32&gt;]<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">To return to the example, you can replace the redirection arrow (or the <b>Out-File<\/b> cmdlet) and the <b>Get-Content<\/b> cmdlet (<b>cat<\/b> alias) with the <b>Tee-Object<\/b> cmdlet. The revised code is seen here: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">Get-WmiObject -Class win32_bios | Tee-Object -FilePath c:fsobios.txt<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">When you run the command, you receive the output shown here:<\/p>\n<p class=\"Fig-Graphic\">\n<p><img decoding=\"async\" title=\"Image of the output received\" alt=\"Image of the output received\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/july\/hey0707\/hsg-07-07-09-02.jpg\" width=\"600\" height=\"211\"><\/p>\n<\/p>\n<p class=\"Num-Caption\"><br>One thing to keep in mind when using the <b>Tee-Object<\/b> is that it will always overwrite the previous text file if the file already exists. If the file does not exist, the <b>Tee-Object<\/b> cmdlet will create the file. The <b>Tee-Object<\/b> cmdlet will not create the folder. If you attempt to use the <b>Tee-Object<\/b> cmdlet to write to a folder that does not exist, you will receive an error that warns of a missing path. This error message is seen here:<\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\"><span>PS C:&gt; Get-WmiObject -class win32_bios | Tee-Object -FilePath C:fso5bios.txt<br>out-file : Could not find a part of the path &#8216;C:fso5bios.txt&#8217;.<br>PS C:&gt;<\/span><\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">You can also use the <b>Tee-Object<\/b> cmdlet to hold the output of a command in a variable. This offers a convenient way to save the information for use later in the script. Here is how you would save the results of a command in a variable, and then display it later without using the <b>Tee-Object<\/b>: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\"><span>PS C:&gt; $bios = Get-WmiObject -class win32_bios<br>PS C:&gt; $bios<\/p>\n<p>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/p>\n<p>PS C:&gt;<\/span><\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">The syntax for the <b>Tee-Object<\/b> when used to store the results of a pipeline in a variable is seen here: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\"><span>Tee-Object [-InputObject &lt;PSObject&gt;] -Variable &lt;String&gt; [-Verbose] [-Debug] <br>[-ErrorAction &lt;ActionPreference&gt;] [-ErrorVariable &lt;String&gt;] [-OutVariable <br>&lt;String&gt;] [-OutBuffer &lt;Int32&gt;]<\/span><\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">To store the results of your <b>Get-WmiObject &ndash;Class Win32_Bios<\/b> command in a variable named <b>$bios<\/b>, you use the following command: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\"><span>PS C:&gt; Get-WmiObject -class win32_bios | Tee-Object -Variable bios<\/p>\n<p><br>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/span><\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">The one thing you need to keep in mind with the <b>Tee-Object<\/b> cmdlet when using the variable parameter is that you do not need to use a dollar sign in front of the variable name. This makes the behavior of the cmdlet the same as when using the <b>New-Variable<\/b> cmdlet. <\/p>\n<p class=\"MsoNormal\">To see the contents of the <b>$bios<\/b> variable, you type it on the command line in the Windows PowerShell console as seen here: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\"><span>PS C:&gt; $bios<\/p>\n<p>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/p>\n<p>PS C:&gt;<\/span><\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">One of the best features of the <b>Tee-Object<\/b> is that it will also pass the object though the pipeline. This means you are not stuck with the default display of information that is returned by the previous command such as the <b>Get-WmiObject<\/b> cmdlet. You can store the object in the <b>$bios<\/b> variable and then choose to display only the <b>name<\/b> property: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -class win32_bios | Tee-Object -Variable bios | <\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">select name<\/p>\n<p>name<br>&#8212;-<br>Default System BIOS<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">To retrieve the object from the variable, you once again type it on the command line or use it elsewhere in your script. This is seen here: <\/p>\n<p class=\"CodeBlock\"><font size=\"1\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; $bios<\/p>\n<p>SMBIOSBIOSVersion : A01<br>Manufacturer<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Dell Computer Corporation<br>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Default System BIOS<br>SerialNumber<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9HQ1S21<br>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: DELL<span>&nbsp;&nbsp; <\/span>&#8211; 6<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">Well, NR, we have come to the end of our discussion on splitting the output from commands to the command line and storing it in a file. In addition, we decided to show you a cool trick that allows you to store objects in variables and manipulate that output as well. Hopefully you have enjoyed this discussion. Join us tomorrow as we continue looking at handling output from within Windows PowerShell. To keep up to date with all the latest Script Center news, follow us on <a href=\"https:\/\/twitter.com\/scriptingguys\/\">Twitter<\/a>. You can also check out the <a href=\"http:\/\/www.facebook.com\/group.php?gid=5901799452\">Scripting Guys on Facebook<\/a>, and join our group to receive notifications of special events. If you find yourself in need of assistance, don&#8217;t forget the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en-US\/ITCG\/threads\">Official Scripting Guys Forum<\/a>. We have members from all around the world, so there is a good chance someone would be awake and willing to lend a hand. Until tomorrow, take care. <\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<\/p>\n<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! You know the old commercial that goes, &#8220;Sometimes you feel like a nut, sometimes you don&#8217;t&#8221;? Well, sometimes I want to save information from a script to a file, and sometimes I want to see it displayed to the screen. And at other times, I want both. In other words, I want [&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":[25,3,4,45],"class_list":["post-53003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! You know the old commercial that goes, &#8220;Sometimes you feel like a nut, sometimes you don&#8217;t&#8221;? Well, sometimes I want to save information from a script to a file, and sometimes I want to see it displayed to the screen. And at other times, I want both. In other words, I want [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53003","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=53003"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53003\/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=53003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}