{"id":2491,"date":"2013-12-03T00:01:00","date_gmt":"2013-12-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/03\/getting-started-with-powershell-text-files\/"},"modified":"2013-12-03T00:01:00","modified_gmt":"2013-12-03T00:01:00","slug":"getting-started-with-powershell-text-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/getting-started-with-powershell-text-files\/","title":{"rendered":"&#65279;Getting Started with PowerShell: Text Files"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with text files.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Good morning! Today I have an excerpt from my new book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-First-Steps\/dp\/0735681007\/ref=la_B001ILFMZ8_1_2_title_1_pap?s=books&amp;ie=UTF8&amp;qid=1383673887&amp;sr=1-2\">Windows PowerShell 3.0 First Steps<\/a>, which was recently published by Microsoft Press.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0257.First%20Steps%20book.jpg\"><img decoding=\"async\" title=\"Image of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0257.First%20Steps%20book.jpg\" alt=\"Image of book cover\" \/><\/a><\/p>\n<p align=\"left\">One of the easiest methods to store data is in a text file. In the following image, the output from the <strong>Get-Volume<\/strong> function displays in the Windows PowerShell console. The output formats nicely in columns, and it contains essential information about the volumes on a Windows&nbsp;8 laptop.&nbsp;<\/p>\n<p class=\"Num-Caption\" align=\"left\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6303.12-3-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6303.12-3-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h3>Redirect and append<\/h3>\n<p align=\"left\">The easiest way to store volume information that is obtained from the <strong>Get-Volume<\/strong> function is to redirect the output to a text file. Because several lines of information return from the function, it is best to redirect and append the outputted information. The redirect and append operator is two right arrows, one behind the other with no space between them.<\/p>\n<p align=\"left\">The following command redirects and appends the information from the <strong>Get-Volume<\/strong> function to a text file that resides in the folder c:\\fso. The file, VolumeInfo.txt, does not have to exist. If it does not exist, it will be created, and the information is written to the file. If the file does exist, the outputted data will append to the file. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\" align=\"left\">Get-Volume &gt;&gt;c:\\fso\\volumeinfo.txt<\/p>\n<p align=\"left\">When the command runs, nothing outputs to the Windows PowerShell console. The output, formatted as it appears in the Windows PowerShell console, writes to the target text file. The following image shows the volumeinfo.txt file that was created by redirecting and appending the results of the <strong>Get-Volume<\/strong> function from Windows&nbsp;8.<\/p>\n<p class=\"BullList\" align=\"left\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8103.12-3-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8103.12-3-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p align=\"left\">If you run the command that redirects and appends the information from the <strong>Get-Volume<\/strong> function to a text file that resides in the folder c:\\fso named volumeinfo.txt a second time, the information from <strong>Get-Volume<\/strong> writes to the bottom of the previously created text file&mdash;that it, it appends to the file. This is a great way to produce simple logging. The following image shows the volume information appearing twice. In both cases, the values are identical. This shows that between the first time the <strong>Get-Volume<\/strong> command ran and the second time <strong>Get-Volume<\/strong> ran, nothing changed.&nbsp;<\/p>\n<p class=\"BullList\" align=\"left\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4251.12-3-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4251.12-3-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h3>Redirect and overwrite<\/h3>\n<p align=\"left\">If you do not need to maintain a history of prior settings, results, or data, use the redirect operator and do not append. The redirect and overwrite operator is a single right arrow. The following command redirects and overwrites the information from the <strong>Get-Volume<\/strong> function to a text file that resides in the folder c:\\fso. The file, VolumeInfo.txt, does not have to exist. If it does not exist, it will be created, and the information is written to the file. If the file does exist, the outputted data will overwrite previously existing data when writing to the file. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\" align=\"left\">Get-Volume &gt;c:\\fso\\volumeinfo.txt<\/p>\n<p align=\"left\">Comparing the <strong>SizeRemaining<\/strong><em> <\/em>value of drive C<em> <\/em>from the following image with the <strong>SizeRemaining<\/strong><em> <\/em>value of the drive C in the previous image reveals that the drive suddenly has nearly 4&nbsp;GB of additional free space. But because the volumeinfo.txt file is overwritten by the redirect and overwrite operator, you would have no way to discover this condition unless you had a backup of the previous volumeinfo.txt file. Knowing when 4&nbsp;GB of disk space suddenly became available might help the Help Desk when a user calls and says, &ldquo;All of a sudden, Outlook is not working.&rdquo;<\/p>\n<p class=\"BullList\" align=\"left\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7853.12-3-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7853.12-3-13-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p align=\"left\">Join me tomorrow when I will have another excerpt from my Microsoft Press book, Windows PowerShell&nbsp;3.0 First Steps.<\/p>\n<p align=\"left\">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 align=\"left\"><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with text files. Microsoft Scripting Guy, Ed Wilson, is here. Good morning! Today I have an excerpt from my new book, Windows PowerShell 3.0 First Steps, which was recently published by Microsoft Press. One of the easiest methods to store data is [&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":[51,3,4,45],"class_list":["post-2491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","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 using Windows PowerShell to work with text files. Microsoft Scripting Guy, Ed Wilson, is here. Good morning! Today I have an excerpt from my new book, Windows PowerShell 3.0 First Steps, which was recently published by Microsoft Press. One of the easiest methods to store data is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2491","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=2491"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2491\/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=2491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}