{"id":1064,"date":"2014-07-04T00:01:00","date_gmt":"2014-07-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/04\/back-to-basics-part-5-outputting-data\/"},"modified":"2022-06-13T11:14:35","modified_gmt":"2022-06-13T18:14:35","slug":"back-to-basics-part-5-outputting-data","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/back-to-basics-part-5-outputting-data\/","title":{"rendered":"Back to Basics Part 5: Outputting Data"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft PFE, Gary Siepser, talks about outputting data via the pipeline in this exciting conclusion to a five part series.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today brings an end to a week of guest blog posts by Gary Siepser. You can read the following posts to catch up with the previous days:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/back-to-the-basics-part-1-learn-about-the-powershell-pipeline\/\" target=\"_blank\" rel=\"noopener\">Back to the Basics Part 1: Learn About the PowerShell Pipeline<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/back-to-the-basics-part-2-learn-about-the-pipeline-and-getters\/\" target=\"_blank\" rel=\"noopener\">Back to the Basics Part 2: Learn about the Pipeline and &#8220;Getters&#8221;<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/back-to-the-basics-part-3-do-something-with-your-data\/\" target=\"_blank\" rel=\"noopener\">Back to the Basics Part\u00a03: Do Something with Your Data<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/back-to-basics-part-4-more-ways-to-manipulate-data\/\" target=\"_blank\" rel=\"noopener\">Back to Basics Part 4: More Ways to Manipulate Data<\/a><\/li>\n<\/ul>\n<p>In Parts 3 and 4 of this series, we talked about a number of different things you can do with data coming down the pipeline. In Part 5, we are going to talk about what you do at the end of a pipeline. This can often be to output or present your data in some way. We\u2019ll focus on formatting your objects to view them a certain way, getting them into simple text files, and exporting to other formats (like CSV and XML).<\/p>\n<p>After you have some data in your pipeline and you have manipulated it the way you want, the last part of the pipeline is to output or present your data. You have likely already noticed that if you choose not to do anything, you will still see your data. Any time you have stuff in the pipeline, if you let that stuff hit the end of the pipeline, Windows PowerShell is going to show it to you in some way.<\/p>\n<p>This is due to the default formatting system in Windows PowerShell. Formatting is simply putting this structured data (your objects) into a more humanly readable form. This usually takes the form of a table or a list.<\/p>\n<p>This default look at our data is great, and I am really glad it is there, but very often we need to see that data presented in a different way. We use the formatting cmdlets to control exactly what that readable rendering should look like. We have the <strong>Format-Table<\/strong>, <strong>Format-List<\/strong>, and <strong>Format-Wide<\/strong> cmdlets to take care of this.<\/p>\n<p>The most common format type I find myself using is a table. Tables are great for comparing object values to each other in a fairly concise way. <strong>Format-Table<\/strong> is our best friend for this. Like most of the <strong>Object<\/strong> cmdlets we have already covered, the format cmdlets are really easy to use. You simply need to tell <strong>Format-Table<\/strong> or <strong>Format-List<\/strong> what properties you want to be shown. <strong>Format-Wide<\/strong> focuses on only one property at a time.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0675.Capture1.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0675.Capture1.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><b style=\"font-size:12px\">Format-Table<\/b><span style=\"font-size:12px\"> has a parameter that I see used all the time called <\/span><b style=\"font-size:12px\">\u2013AutoSize<\/b><span style=\"font-size:12px\">. Generally, it squeezes everything to the left as tight as it can to fit more in. It also helps with values that are being truncated like the ones you see in the previous example.<\/span><\/p>\n<p>Sometimes though, if you have really wide values in your columns, <strong>-AutoSize<\/strong> can backfire and cause columns on the right to be left off completely because there is not enough space. Notice the in the following example that the <strong>Status<\/strong> column didn\u2019t make the result because of<strong>\u2013AutoSize<\/strong>.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6354.Capture2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6354.Capture2.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><span style=\"font-size:12px\">Over the years I have encountered a number of folks who would like to see <\/span><b style=\"font-size:12px\">\u2013AutoSize<\/b><span style=\"font-size:12px\"> turned on by default because they use it almost every time they use <\/span><b style=\"font-size:12px\">Format-Table<\/b><span style=\"font-size:12px\">. Introduced in Windows PowerShell\u00a03.0, you can run the following command to set the parameter defaults:<\/span><\/p>\n<p>$PSDefaultParameterValues[&#8216;Format-Table:Autosize&#8217;] = $true<br \/>\nNow, when you run <strong>Format-Table<\/strong>, the output will always be \u201cautosized\u201d! This setting will be lost when you close Windows PowerShell, but you can set it in a profile script to have it run each time you launch Windows PowerShell. For more information, see Help about profiles (Get-Help About_Profiles \u2013ShowWindow).<\/p>\n<p>Tables are great, but they are limited by the amount of width you have. There is a <strong>\u2013Wrap<\/strong> parameter that can help with the width issue, but still there is only so much space in a table. This is where a list comes in to play.<\/p>\n<p>I think of <strong>Format-List<\/strong> as the report writer. It gives you a vertical list of objects and properties. Each property has as much width as it needs and will wrap to next line if it needs to. Like <strong>Format-Table<\/strong>, <strong>Format-List<\/strong> needs to know which properties you want to show up.<\/p>\n<p>When you are exploring objects beyond using <strong>Get-Member<\/strong> (discussed in Part\u00a03), you can use <strong>Format-List<\/strong> followed by an asterisk ( *** <strong>) to represent all properties. Take a look at these examples for **Format-List<\/strong>:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8473.Capture3.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8473.Capture3.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><strong>Format-Wide<\/strong> is really useful when you need to see only one thing, such as the <strong>Name<\/strong> property, but you want to squeeze as many objects on the screen as you can. Like the <strong>Format-Table<\/strong> cmdlet, there is <strong>an \u2013AutoSize<\/strong> parameter to allow <strong>Format-Wide<\/strong> to maximize the numbers of columns without truncating your data.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7802.Capture4.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7802.Capture4.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><span style=\"font-size:12px\">Now that we know how to format our data to see exactly what we want to see, let\u2019s see about how we can record this into a text file. There are a couple of different choices for this. I prefer the <\/span><b style=\"font-size:12px\">Out-File<\/b><span style=\"font-size:12px\"> cmdlet, but there is also a <\/span><b style=\"font-size:12px\">Set-Content<\/b><span style=\"font-size:12px\"> cmdlet.<\/span><\/p>\n<p>The biggest difference between the cmdlets is that <strong>Out-File<\/strong> can come after formatting in the pipeline, but <strong>Set-Content<\/strong> cannot. <strong>Set-Content<\/strong> only works well when the objects coming through the pipeline are already strings. <strong>Out-File<\/strong> records whatever you could normally see in the Windows PowerShell console, so I think it\u2019s the better cmdlet, especially for beginners.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5556.Capture5.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5556.Capture5.PNG\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p style=\"margin-left:30px\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-4-14-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-4-14-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a>\n<\/p>\n<p>Text files are great for log type files and simple long term keeping, but sometimes we need a richer data format. Many IT admins are already familiar with a format called CSV. CSV format is a text file that contains rows of values that are delimited by a character (a comma by default). However, the delimiter can be any character. Commas, tabs, and pipes seem to be the most common, in my experience.<\/p>\n<p>We discussed <strong>the Import-CSV<\/strong> cmdlet in Part 2. Now we will look at the <strong>Export-CSV<\/strong> cmdlet. It\u2019s a really simple cmdlet. Whatever gets piped in to it gets converted to simple text in the CSV file you create. It will export all the properties in the pipeline to that CSV file.<\/p>\n<p>Remember, if you look at your objects without formatting in Windows PowerShell, you are going to see the default formatting we learned about earlier in this post. This is usually a reduced set of the properties for your objects\u2014generally in some decent looking format.<\/p>\n<p><strong>Export-CSV<\/strong> will export ALL the properties for your objects, so you might end up with an unexpected amount of data in the CSV file. The great news is, if you have been following this series, you already know how to deal with this. The <strong>Select-Object<\/strong> cmdlet that we covered in Part 3 allows you to reduce the data to only what you want in the pipeline prior to running the <strong>Export-CSV<\/strong>:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/CaptureA.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/CaptureA.PNG\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p style=\"margin-left:30px\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-4-14-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-4-14-2.png\" alt=\"Image of command output\" width=\"550\" height=\"264\" title=\"Image of command output\" \/><\/a>\n<\/p>\n<p style=\"margin-left:30px\">\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/CaptureB.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/CaptureB.PNG\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p style=\"margin-left:30px\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-4-14-3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-4-14-3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a>\n<\/p>\n<p>You can see in the first example that there is a lot of data in the CSV file. This is because process objects in the pipeline have a large number of properties and <strong>Export-CSV<\/strong> lists them all. In the second example, you can see how the data is simplified because <strong>Select-Object<\/strong> is in the pipeline prior to the export, and it stripped the data to only the three selected properties. You can also see in the second example that I exported the CSV file by using a tab as the delimiter instead of the default comma.<\/p>\n<p>Another confusing thing about <strong>Export-CSV<\/strong> is the <strong>#TYPE &#8230;<\/strong> line at the top of file. This line is used specifically when Windows PowerShell imports that file, but it could cause issues if you plan to use the CSV file with some other system that won\u2019t understand the line. <strong>Export-CSV<\/strong> has a parameter switch called <strong>\u2013NoTypeInformation<\/strong> that you can use to cause Windows PowerShell to leave off that <strong>#TYPE &#8230;<\/strong> line.<\/p>\n<p>Many of the things we run in Windows PowerShell are lost when Windows PowerShell is closed. Now that we are storing data in the file system (which persists beyond a closed Windows PowerShell window), we might notice that this is an interesting way we can \u201csave\u201d or \u201cpersist\u201d our objects that are using a CSV file. One limitation is that all values import as strings from a CSV file. A format that gets around this limitation is XML, and you can use the <strong>Export-CliXML<\/strong> cmdlet.<\/p>\n<p>The <strong>Export-CliXML<\/strong> cmdlet works like <strong>Export-CSV<\/strong>, but it uses a much richer and more complex XML format for the file. This format retains more information about the objects, including the data type of the properties.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7077.Capture6.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7077.Capture6.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><span style=\"font-size:12px\">When you look at these examples, you can see how we can export to a rich format like XML (we are only looking at the first 10 lines), and then bring that data back in to Windows PowerShell and use it as if we just ran the original <\/span><b style=\"font-size:12px\">Get-Process<\/b><span style=\"font-size:12px\">.<\/span><\/p>\n<p>There are so many things you can get done with your pipeline. This post has investigated some of the output abilities, but take time to explore all the core commands in Windows PowerShell.<\/p>\n<p>I\u2019ve been using Windows PowerShell for years. On a daily basis, I still feel like I discover cool new things I can do with Windows PowerShell, and especially the pipeline.<\/p>\n<p>As I bring this series to a close, I hope you can see that the pipeline in Windows PowerShell is not simply a way avoid learning how to script. The pipeline is this wonderfully intuitive result of well-designed, single-purpose commands, coupled with the object-based nature of Windows PowerShell. It\u2019s a great way for you to accomplish so much with so little. And you can do it in a way that is extremely readable and easy to modify and maintain going forward.<\/p>\n<p>Go forth and solve the world\u2019s problems one pipeline at a time. There is some Windows PowerShell geekiness for you!<\/p>\n<p>~Gary<\/p>\n<p>Thanks, Gary, for such a great series this week.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size:12px\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft PFE, Gary Siepser, talks about outputting data via the pipeline in this exciting conclusion to a five part series. Microsoft Scripting Guy, Ed Wilson, is here. Today brings an end to a week of guest blog posts by Gary Siepser. You can read the following posts to catch up with the previous days: [&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":[317,51,56,149,3,45],"class_list":["post-1064","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-gary-siepser","tag-getting-started","tag-guest-blogger","tag-pipeline","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft PFE, Gary Siepser, talks about outputting data via the pipeline in this exciting conclusion to a five part series. Microsoft Scripting Guy, Ed Wilson, is here. Today brings an end to a week of guest blog posts by Gary Siepser. You can read the following posts to catch up with the previous days: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1064","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=1064"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1064\/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=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}