{"id":82,"date":"2016-05-03T10:10:29","date_gmt":"2016-05-03T17:10:29","guid":{"rendered":"http:\/\/vicds.myds.me\/wordpress\/?p=82"},"modified":"2020-08-21T13:24:47","modified_gmt":"2020-08-21T20:24:47","slug":"psreadcsv","status":"publish","type":"post","link":"https:\/\/www.vscrypt.com\/psreadcsv\/","title":{"rendered":"Reading a CSV File Quickly Using PowerShell"},"content":{"rendered":"\n<p>Often time we need to import data from CSV files into PowerShell to perform additional tasks. \u00a0In this post, I would like to share one of the ways I use PowerShell to read values from CSV files. \u00a0I am not saying this is the only best way but I like this way because of its quick. \u00a0I hope you find this helpful.<\/p>\n\n\n\n<p>So, let&#8217;s say you want to read a CSV file called <strong>users.csv<\/strong> that has the following columns as shown above.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Excel\" data-shcb-language-slug=\"excel\"><span><code class=\"hljs language-excel\">LoginName\nDisplayName\nUserPrincipalName<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Excel<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">excel<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Create a new .ps1 file with the following and execute it from the PowerShell window.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PowerShell\" data-shcb-language-slug=\"powershell\"><span><code class=\"hljs language-powershell shcb-code-table shcb-line-numbers shcb-wrap-lines\"><span class='shcb-loc'><span><span class=\"hljs-comment\"># Specify the source file name<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-variable\">$SourceFile<\/span> = c:\\users.csv\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-comment\"># Define your Array variables<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-variable\">$strName<\/span> = <span class=\"hljs-selector-tag\">@<\/span>()\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-variable\">$strDispName<\/span> = <span class=\"hljs-selector-tag\">@<\/span>()\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-variable\">$strUPN<\/span> = <span class=\"hljs-selector-tag\">@<\/span>()\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-comment\"># Read file data to arrays<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-built_in\">Import-Csv<\/span> <span class=\"hljs-variable\">$SourceFile<\/span> |\n<\/span><\/span><span class='shcb-loc'><span>     <span class=\"hljs-built_in\">ForEach-Object<\/span> {\n<\/span><\/span><span class='shcb-loc'><span>          <span class=\"hljs-variable\">$strName<\/span> += <span class=\"hljs-variable\">$_<\/span>.LoginName\n<\/span><\/span><span class='shcb-loc'><span>          <span class=\"hljs-variable\">$strDispName<\/span> += <span class=\"hljs-variable\">$_<\/span>.DisplayName\n<\/span><\/span><span class='shcb-loc'><span>          <span class=\"hljs-variable\">$strUPN<\/span> += <span class=\"hljs-variable\">$_<\/span>.UserPrincipalName\n<\/span><\/span><span class='shcb-loc'><span>}\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-comment\"># Count the number of values<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-variable\">$cntValues<\/span> = <span class=\"hljs-variable\">$strName<\/span>.Length\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-comment\"># This is just to define the variable used in the For loop below<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-variable\">$i<\/span> = <span class=\"hljs-number\">0<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-comment\"># Enumerate through the arrays to use each value<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-variable\">$i<\/span>=<span class=\"hljs-number\">0<\/span>; <span class=\"hljs-variable\">$i<\/span> <span class=\"hljs-operator\">-lt<\/span> <span class=\"hljs-variable\">$cntValues<\/span>; <span class=\"hljs-variable\">$i<\/span>++)\n<\/span><\/span><span class='shcb-loc'><span>{\n<\/span><\/span><span class='shcb-loc'><span>     <span class=\"hljs-comment\"># Retrieve each row from the array<\/span>\n<\/span><\/span><span class='shcb-loc'><span>     <span class=\"hljs-variable\">$strMyName<\/span> = <span class=\"hljs-variable\">$strName<\/span>&#91;<span class=\"hljs-variable\">$i<\/span>].ToString()\n<\/span><\/span><span class='shcb-loc'><span>     <span class=\"hljs-variable\">$strMyDispName<\/span> = <span class=\"hljs-variable\">$strDispName<\/span>&#91;<span class=\"hljs-variable\">$i<\/span>].ToString()\n<\/span><\/span><span class='shcb-loc'><span>     <span class=\"hljs-variable\">$strMyUPN<\/span> = <span class=\"hljs-variable\">$strUPN<\/span>&#91;<span class=\"hljs-variable\">$i<\/span>].ToString()\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>     <span class=\"hljs-comment\"># Then do whatever you like with the value from each row.<\/span>\n<\/span><\/span><span class='shcb-loc'><span>     <span class=\"hljs-comment\"># ...<\/span>\n<\/span><\/span><span class='shcb-loc'><span>}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PowerShell<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">powershell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Often time we need to import data from CSV files into PowerShell to perform additional tasks. \u00a0In this post, I would like to share one &hellip; <\/p>\n","protected":false},"author":1,"featured_media":83,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[15,9,16],"class_list":["post-82","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ps","category-scripts","tag-csv-file","tag-powershell","tag-read-csv"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Reading a CSV File Quickly Using PowerShell - VScrypt<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vscrypt.com\/psreadcsv\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reading a CSV File Quickly Using PowerShell - VScrypt\" \/>\n<meta property=\"og:description\" content=\"Often time we need to import data from CSV files into PowerShell to perform additional tasks. \u00a0In this post, I would like to share one &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vscrypt.com\/psreadcsv\/\" \/>\n<meta property=\"og:site_name\" content=\"VScrypt\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-03T17:10:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-21T20:24:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"359\" \/>\n\t<meta property=\"og:image:height\" content=\"167\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vikram\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vikram\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/\"},\"author\":{\"name\":\"Vikram\",\"@id\":\"https:\/\/www.vscrypt.com\/#\/schema\/person\/855a3e6d9310019d20f2da32115df36a\"},\"headline\":\"Reading a CSV File Quickly Using PowerShell\",\"datePublished\":\"2016-05-03T17:10:29+00:00\",\"dateModified\":\"2020-08-21T20:24:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/\"},\"wordCount\":105,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.vscrypt.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg\",\"keywords\":[\"CSV File\",\"powershell\",\"Read CSV\"],\"articleSection\":[\"PowerShell\",\"Scripts\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.vscrypt.com\/psreadcsv\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/\",\"url\":\"https:\/\/www.vscrypt.com\/psreadcsv\/\",\"name\":\"Reading a CSV File Quickly Using PowerShell - VScrypt\",\"isPartOf\":{\"@id\":\"https:\/\/www.vscrypt.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg\",\"datePublished\":\"2016-05-03T17:10:29+00:00\",\"dateModified\":\"2020-08-21T20:24:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vscrypt.com\/psreadcsv\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage\",\"url\":\"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg\",\"contentUrl\":\"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg\",\"width\":359,\"height\":167,\"caption\":\"Excel File\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vscrypt.com\/psreadcsv\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vscrypt.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Reading a CSV File Quickly Using PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vscrypt.com\/#website\",\"url\":\"https:\/\/www.vscrypt.com\/\",\"name\":\"VScrypt\",\"description\":\"Helpful scripts, snippets and guides...\",\"publisher\":{\"@id\":\"https:\/\/www.vscrypt.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vscrypt.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vscrypt.com\/#organization\",\"name\":\"VScrypt\",\"url\":\"https:\/\/www.vscrypt.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vscrypt.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2019\/01\/logo-1.png\",\"contentUrl\":\"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2019\/01\/logo-1.png\",\"width\":200,\"height\":80,\"caption\":\"VScrypt\"},\"image\":{\"@id\":\"https:\/\/www.vscrypt.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.vscrypt.com\/#\/schema\/person\/855a3e6d9310019d20f2da32115df36a\",\"name\":\"Vikram\",\"description\":\"I am just an IT person trying to share as much knowledge as possible hoping someone will find it helpful. It just takes time to organize everything from my head into something that makes sense.\",\"url\":\"https:\/\/www.vscrypt.com\/author\/vchand\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reading a CSV File Quickly Using PowerShell - VScrypt","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vscrypt.com\/psreadcsv\/","og_locale":"en_US","og_type":"article","og_title":"Reading a CSV File Quickly Using PowerShell - VScrypt","og_description":"Often time we need to import data from CSV files into PowerShell to perform additional tasks. \u00a0In this post, I would like to share one &hellip;","og_url":"https:\/\/www.vscrypt.com\/psreadcsv\/","og_site_name":"VScrypt","article_published_time":"2016-05-03T17:10:29+00:00","article_modified_time":"2020-08-21T20:24:47+00:00","og_image":[{"width":359,"height":167,"url":"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg","type":"image\/jpeg"}],"author":"Vikram","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Vikram","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.vscrypt.com\/psreadcsv\/#article","isPartOf":{"@id":"https:\/\/www.vscrypt.com\/psreadcsv\/"},"author":{"name":"Vikram","@id":"https:\/\/www.vscrypt.com\/#\/schema\/person\/855a3e6d9310019d20f2da32115df36a"},"headline":"Reading a CSV File Quickly Using PowerShell","datePublished":"2016-05-03T17:10:29+00:00","dateModified":"2020-08-21T20:24:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.vscrypt.com\/psreadcsv\/"},"wordCount":105,"commentCount":0,"publisher":{"@id":"https:\/\/www.vscrypt.com\/#organization"},"image":{"@id":"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg","keywords":["CSV File","powershell","Read CSV"],"articleSection":["PowerShell","Scripts"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.vscrypt.com\/psreadcsv\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.vscrypt.com\/psreadcsv\/","url":"https:\/\/www.vscrypt.com\/psreadcsv\/","name":"Reading a CSV File Quickly Using PowerShell - VScrypt","isPartOf":{"@id":"https:\/\/www.vscrypt.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage"},"image":{"@id":"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg","datePublished":"2016-05-03T17:10:29+00:00","dateModified":"2020-08-21T20:24:47+00:00","breadcrumb":{"@id":"https:\/\/www.vscrypt.com\/psreadcsv\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vscrypt.com\/psreadcsv\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vscrypt.com\/psreadcsv\/#primaryimage","url":"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg","contentUrl":"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2016\/05\/2016-05-03_10-08-38.jpg","width":359,"height":167,"caption":"Excel File"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vscrypt.com\/psreadcsv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vscrypt.com\/"},{"@type":"ListItem","position":2,"name":"Reading a CSV File Quickly Using PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/www.vscrypt.com\/#website","url":"https:\/\/www.vscrypt.com\/","name":"VScrypt","description":"Helpful scripts, snippets and guides...","publisher":{"@id":"https:\/\/www.vscrypt.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vscrypt.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vscrypt.com\/#organization","name":"VScrypt","url":"https:\/\/www.vscrypt.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vscrypt.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2019\/01\/logo-1.png","contentUrl":"https:\/\/www.vscrypt.com\/wp-content\/uploads\/2019\/01\/logo-1.png","width":200,"height":80,"caption":"VScrypt"},"image":{"@id":"https:\/\/www.vscrypt.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.vscrypt.com\/#\/schema\/person\/855a3e6d9310019d20f2da32115df36a","name":"Vikram","description":"I am just an IT person trying to share as much knowledge as possible hoping someone will find it helpful. It just takes time to organize everything from my head into something that makes sense.","url":"https:\/\/www.vscrypt.com\/author\/vchand\/"}]}},"_links":{"self":[{"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/comments?post=82"}],"version-history":[{"count":0,"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/posts\/82\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/media\/83"}],"wp:attachment":[{"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vscrypt.com\/api\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}