{"id":12181,"date":"2011-11-05T00:01:00","date_gmt":"2011-11-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/05\/use-powershell-to-read-csv-files-and-create-files-and-folders\/"},"modified":"2011-11-05T00:01:00","modified_gmt":"2011-11-05T00:01:00","slug":"use-powershell-to-read-csv-files-and-create-files-and-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-read-csv-files-and-create-files-and-folders\/","title":{"rendered":"Use PowerShell to Read CSV Files and Create Files and Folders"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to use Windows PowerShell to read CSV files and create files and folders.<\/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. This past week has been fun. I enjoy talking about comma-separated value files (CSV) because they are very flexible, and Windows PowerShell makes working with them really easy. <\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">One of the cool things that can be done using a CSV file was only alluded to in yesterday&rsquo;s post, <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/11\/04\/use-a-csv-file-to-populate-parameters-of-powershell-cmdlets.aspx\"><span style=\"font-family: Segoe\">Use a CSV File to Populate Parameters of PowerShell Cmdlets<\/span><\/a><span style=\"font-family: Segoe\">. <\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">In yesterday&rsquo;s article, I piped information from a CSV file to the <b>Get-WmiObject<\/b> cmdlet. The problem is that the <b>Get-WmiObject<\/b> cmdlet does not accept piped input for either the <i>class <\/i>or <i>computername <\/i>parameter. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">However, the <b>New-Item<\/b> cmdlet does accept piped input. When a cmdlet accepts piped input, it will automatically match column names from a CSV file with parameter names of the cmdlet receiving the piped input. This allows for some extremely powerful scenarios. Here are the steps for this scenario:<\/span><\/p>\n<ol>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Create a CSV file that uses column headings that are the same as the parameter names for the cmdlet to use.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Use the <b>Import-CSV<\/b> cmdlet to read the CSV file and to create a custom object from that file.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Pipe the results to a cmdlet that accepts piped input.<\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: Segoe;font-size: small\">Suppose I create a CSV file that contains a file system layout. In that file, I specify the name of the folder and the names of files I want to create in that folder. I use the column names of <b>path<\/b> and <b>itemtype<\/b> because those are the parameter names used by the <b>New-Item<\/b> cmdlet. A sample CSV file that meets these requirements 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\/4101.hsg-11-5-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of CSV file that meets requirements\" alt=\"Image of CSV file that meets requirements\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4101.hsg-11-5-11-01.png\" width=\"427\" height=\"435\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After I have created my file, the Windows PowerShell command itself is trivial. I use the <b>Import-CSV<\/b> cmdlet to read the CSV file, and I pipe the results to the <b>New-Item<\/b> cmdlet. This command is 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 use of the command to read a CSV file and create files and folders along with the 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\/3286.hsg-11-5-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\/3286.hsg-11-5-11-02.png\" width=\"522\" height=\"280\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">When this command runs, the following is displayed in Windows Explorer.<\/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\/2577.hsg-11-5-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of what is displayed in Windows Explorer when command runs\" alt=\"Image of what is displayed in Windows Explorer when command runs\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2577.hsg-11-5-11-03.png\" width=\"525\" height=\"419\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">That is about all there is to reading a CSV file and automatically creating files and folders. Given the Windows PowerShell providers that work with <b>New-Item<\/b>, this one scenario and one cmdlet provide lots of possibilities. And this is only one cmdlet. I will leave it to you to explore and see what you can come up with. Let me know. <\/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><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/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 read CSV files and create files and folders. &nbsp; Microsoft Scripting Guy Ed Wilson here. This past week has been fun. I enjoy talking about comma-separated value files (CSV) because they are very flexible, and Windows PowerShell makes working with them really easy. One of the cool [&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-12181","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 read CSV files and create files and folders. &nbsp; Microsoft Scripting Guy Ed Wilson here. This past week has been fun. I enjoy talking about comma-separated value files (CSV) because they are very flexible, and Windows PowerShell makes working with them really easy. One of the cool [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12181","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=12181"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12181\/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=12181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}