{"id":12171,"date":"2011-11-06T00:01:00","date_gmt":"2011-11-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/06\/use-powershell-to-automatically-create-a-csv-file\/"},"modified":"2011-11-06T00:01:00","modified_gmt":"2011-11-06T00:01:00","slug":"use-powershell-to-automatically-create-a-csv-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-automatically-create-a-csv-file\/","title":{"rendered":"Use PowerShell to Automatically Create a CSV File"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to use Windows PowerShell to automatically create a CSV file.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Microsoft Scripting Guy Ed Wilson here. I am about finished playing around with CSV files for the time being. I do not always use the <b>ConvertTo-CSV<\/b> or the <b>Export-CSV<\/b> cmdlet. There are times that I write code and create my own CSV file from scratch. An example of this is the CreateCSVFile.ps1 script that follows:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">CreateCSVFile.ps1<\/span><\/b><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">1..5 | ForEach-Object -begin { &#8220;path,itemtype&#8221; } -process{ <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&#8220;c:\\folder$_,directory&#8221; <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;for($i=0; $i -le 10; $i++) {&#8220;c:\\folder$_\\file$i.txt,file&#8221;}} | <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;Out-File c:\\fso\\filesAndFolders.csv -Encoding ascii -Force<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">This script creates a CSV file that I can use to create a nested folder hierarchy. It is useful when testing certain applications, or for testing a Windows PowerShell script. I create five folders, and inside each folder I create 11 files. The folders are off the root and are named Folder1, Folder2, Folder3, Folder4, and Folder5. The files are named file0, file1, and so on through file10.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">When using the <b>Foreach-Object<\/b>, I use the <i>begin <\/i>parameter to perform an action once. Inside the process block, I create the path to the folder, and I include the <b>itemtype<\/b> of directory. Next, I use a loop to create the path to the files. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The newly created CSV file is shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6404.hsg-11-6-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of newly created CSV file\" alt=\"Image of newly created CSV file\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6404.hsg-11-6-11-01.png\" width=\"429\" height=\"550\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I use the <b>Import-CSV<\/b> cmdlet to import the CSV file, and I pipe it to the <b>New-Item<\/b> cmdlet as shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Import-Csv C:\\fso\\FilesAndFolders.csv | New-Item<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0624.hsg-11-6-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0624.hsg-11-6-11-02.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">One of the folders and associated files is shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0020.hsg-11-6-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of folder and files\" alt=\"Image of folder and files\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0020.hsg-11-6-11-03.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">As you can see, there are times when it is easier to use a little bit of code to create the CSV file. This makes it a lot easier when it comes time to import the file and to pipeline it to another cmdlet.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">This is all for today. I hope you have a great day. See you next week.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"font-family: Segoe\">Twitter<\/span><\/a><span style=\"font-family: Segoe\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-family: Segoe\">Facebook<\/span><\/a><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"font-family: Segoe\">scripter@microsoft.com<\/span><\/a><span style=\"font-family: Segoe\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span style=\"font-family: Segoe\">Official Scripting Guys Forum<\/span><\/a><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to automatically create a CSV file. &nbsp; Microsoft Scripting Guy Ed Wilson here. I am about finished playing around with CSV files for the time being. I do not always use the ConvertTo-CSV or the Export-CSV cmdlet. There are times that I write code and create my own [&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":[169,3,4,61,45],"class_list":["post-12171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-csv-and-other-delimited-files","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to automatically create a CSV file. &nbsp; Microsoft Scripting Guy Ed Wilson here. I am about finished playing around with CSV files for the time being. I do not always use the ConvertTo-CSV or the Export-CSV cmdlet. There are times that I write code and create my own [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12171","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=12171"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12171\/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=12171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}