{"id":194,"date":"2008-10-04T07:36:22","date_gmt":"2008-10-04T07:36:22","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=194"},"modified":"2022-10-31T02:59:45","modified_gmt":"2022-10-31T02:59:45","slug":"powershell-retrieving-data-from-excel","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/powershell-retrieving-data-from-excel","title":{"rendered":"powershell: Retrieving data from Excel"},"content":{"rendered":"<blockquote>\n<p><span style=\"font-size: medium;\">Note June 2011: This post gets a lot of hits but is very old now. I haven&#8217;t revisited the powershell-Excel story since writing this post and it may well be a lot easier now.<\/span><\/p>\n<\/blockquote>\n<p>I would love nothing better than to do one ILM project after another, but it doesn&#8217;t always pan out like that, so now I&#8217;m back to the bread-and-butter work of server installations, email migrations and security audits. Still, it&#8217;s giving me an opportunity to get my powershell skills up to date.<\/p>\n<p>This post, after some blurb, includes a script I wrote to extract data from an Excel spreadsheet. At this point I don&#8217;t think I&#8217;ll actually use it, but it was an interesting exercise.<\/p>\n<p><!--more--><\/p>\n<p>Like most people, I find it frustrating learning new methods when the old way would be much quicker. I&#8217;ve been sticking firmly to vbscript, but Exchange 2007 has forced me into this alleged shell of power.<\/p>\n<p>Some of my frustration has, I think, been perfectly valid. Just try setting file system ACLs. Set-acl just does not do enough &#8211; for instance you can&#8217;t block inheritance. You can call cacls from powershell, but just try using the &#8220;&lt;&#8221; to pipe in the &#8220;y&#8221; character and suppress the prompting &#8211; powershell politely informs you that it doesn&#8217;t support &#8220;&lt;&#8221; redirection. Big raspberry.<\/p>\n<p>Other things, however, it does do very well indeed. I love the handling of CSV files. This command<\/p>\n<pre>import-csv file.csv | foreach { ... }<\/pre>\n<p>is both elegant and really, really useful.<\/p>\n<p>But on to the subject of this post. Similar to a <a href=\"https:\/\/www.wapshere.com\/missmiis\/?p=84\">vbscript I wrote<\/a> for another customer, I was asked to get some info into AD based on an excel spreadsheet. Time to figure out how to do this in powershell.<\/p>\n<p>First off, I ran into an <a href=\"http:\/\/support.microsoft.com\/kb\/320369\" target=\"_blank\" rel=\"noopener noreferrer\">irritating bug<\/a> based on the culture settings. If you are using english Excel you have to set your culture to English<strong>(US)<\/strong> to be able to interact with Excel from powershell. Another big raspberry to Microsoft for that one!<\/p>\n<p>Next I struggled with the usual lack of documentation on the COM object properties and methods, and the fact that they don&#8217;t seem to work the same in powershell as vbscript. For instance in vbscript I could retrieve the top-left cell value by doing this:<\/p>\n<pre>objExcel.Cells(1,1).Value<\/pre>\n<p>but in powershell I was told that method didn&#8217;t exist, and the only way I could get at the data was by instatiating a worksheet object and doing this:<\/p>\n<pre>$objSheet.Cells.Item(1,1).Text<\/pre>\n<p>Why the difference? I really don&#8217;t know.<\/p>\n<p>The script below was more an exercise that anything else. What with the culture bug and some other considerations I think I&#8217;ll end up using a CSV approach to the actual task, but I think this is worth posting just to show how it&#8217;s done.<\/p>\n<pre># import_excel.ps1\n# Retrieves data from a mult-worksheet Excel file\n# and writes to the console in csv format.<\/pre>\n<pre>$strExcelFile = \"MySpreadsheet.xls\"<\/pre>\n<pre>$objExcel = New-Object -comobject Excel.Application\n$objWorkbook = $objExcel.Workbooks.Open($strExcelFile)<\/pre>\n<pre>write-host \"Numer of worksheets: \" $objWorkbook.Sheets.Count<\/pre>\n<pre>$row = 1\n$col = 1\n$sheet = 1<\/pre>\n<pre>while ($sheet -le $objWorkbook.Sheets.Count)\n{\n\u00a0 $objSheet = $objWorkbook.Sheets.Item($sheet)\n\u00a0 write-Host \"Worksheet: \" $objSheet.Name<\/pre>\n<pre>  #Count number of columns in row 1 until first blank\n\u00a0 $numCols = 0\n\u00a0 while ($objSheet.Cells.Item(1,$col).Text -ne \"\")\n\u00a0 { $col += 1 ; $numCols += 1 }\n\u00a0 $col = 1<\/pre>\n<pre>  #Iterate through rows, stopping at the first blank in col 1\n\u00a0 while ($objSheet.Cells.Item($row,1).Text -ne \"\")\n\u00a0 {\n  \u00a0 $strLine = \"\"\n  \u00a0 while ($col -le $numCols)\n    \u00a0 {\n       \u00a0 if ($col -eq 1) {$strLine = $objSheet.Cells.Item($row,$col).Text}\n       \u00a0 else {$strLine = $strLine + \",\" + $objSheet.Cells.Item($row,$col).Text}\n       \u00a0 $col += 1\n    \u00a0 }\n  \u00a0 write-host $strLine\n  \u00a0 $row += 1\n  \u00a0 $col = 1\n\u00a0 }\n\n\u00a0 $sheet += 1\n\u00a0 $row = 1\n\u00a0 $col = 1\n\u00a0 write-host\n}<\/pre>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note June 2011: This post gets a lot of hits but is very old now. I haven&#8217;t revisited the powershell-Excel story since writing this post and it may well be a lot easier now. I would love nothing better than to do one ILM project after another, but it doesn&#8217;t always pan out like that,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[38,23],"tags":[70],"class_list":["post-194","post","type-post","status-publish","format-standard","hentry","category-excel","category-powershell","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-38","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=194"}],"version-history":[{"count":8,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":3309,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/194\/revisions\/3309"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}