{"id":66513,"date":"2006-09-08T15:24:00","date_gmt":"2006-09-08T15:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/08\/how-can-i-use-windows-powershell-to-automate-microsoft-excel\/"},"modified":"2006-09-08T15:24:00","modified_gmt":"2006-09-08T15:24:00","slug":"how-can-i-use-windows-powershell-to-automate-microsoft-excel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-windows-powershell-to-automate-microsoft-excel\/","title":{"rendered":"How Can I Use Windows PowerShell to Automate Microsoft Excel?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I use Windows PowerShell to automate Microsoft Excel?<BR><BR>&#8212; MW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, MW. Yes, it\u2019s true: the week of November 6-10, 2006 will be Windows PowerShell Week on TechNet, with the Scripting Guys presenting a series of webcasts (one each day, Monday through Friday) designed to introduce you to Windows PowerShell. Those are all the details at the moment (we\u2019ll keep you posted), but you can bet that the week will be filled with fun and excitement as we show how to carry out all sorts of useful system administration tasks using Windows PowerShell.<\/P>\n<P>What\u2019s that? Well, we suppose it <I>is<\/I> like a dream come true, assuming that you always dream about Windows PowerShell, of course. And no, that\u2019s OK: all things considered, we\u2019d just as soon <I>not<\/I> know what it is you always dream about.<\/P>\n<TABLE class=\"dataTable\" id=\"EDD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. What do the <I>Scripting Guys<\/I> dream about? Surprisingly enough, none of the Scripting Guys dream. But, then again, maybe that\u2019s not so surprising: after all, when you work at Microsoft you\u2019re <I>living<\/I> the dream.<\/P>\n<P>More or less.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So, MW, are we going to make you wait until November before we show you how you can use Windows PowerShell to automate Microsoft Excel? Well, to tell you the truth, that was the original plan. But then we realized that we\u2019d have to go read a few more emails, that we\u2019d have pick out a different question to answer, that we\u2019d \u2013 well, needless to say, that sounded like an awful lot of work. Especially when you consider how easy it was to just write a sample script that uses Windows PowerShell to automate Microsoft Excel:<\/P><PRE class=\"codeSample\">$a = New-Object -comobject Excel.Application<\/p>\n<p>$a.Visible = $True<\/p>\n<p>$b = $a.Workbooks.Add()\n$c = $b.Worksheets.Item(1)<\/p>\n<p>$c.Cells.Item(1,1) = &#8220;A value in cell A1.&#8221;\n$b.SaveAs(&#8220;C:\\Scripts\\Test.xls&#8221;)<\/p>\n<p>$a.Quit()\n<\/PRE>\n<P>The key to this script occurs in the very first line, the one where we use the <B>New-Object<\/B> Cmdlet to create an instance of Microsoft Excel. Notice that we need to pass New-Object two items: the <B>\u2013comobject<\/B> parameter, which tells the Cmdlet that we want to create a new COM object (as opposed to, say, a new .NET Framework object); and <B>Excel.Application<\/B>, the ProgID of the object we want to create. And no, we didn\u2019t make a mistake (well, not here anyway): you don\u2019t need to enclose the ProgID in double quote marks.<\/P>\n<P>And you wondered why people were so excited about Windows PowerShell!<\/P>\n<P>In effect, line 1 is equivalent to this line of VBScript code:<\/P><PRE class=\"codeSample\">Set a = CreateObject(&#8220;Excel.Application&#8221;)\n<\/PRE>\n<P>Although the lines are functionally equivalent, there are a couple differences we should point out. For one, in Windows PowerShell we don\u2019t have to use the Set keyword (or some equivalent) in order to create an object reference. For another, we <I>do<\/I> need to use a variable name like $a; in Windows PowerShell variable names must begin with a dollar sign ($).<\/P>\n<P>Minor things, sure, but things you need to know.<\/P>\n<P>Now, back to the script. In the next line we set the <B>Visible<\/B> property of Excel to true, thus enabling us to see the application onscreen:<\/P><PRE class=\"codeSample\">$a.Visible = $True\n<\/PRE>\n<P>That should be reasonably familiar: you set property values in Windows PowerShell using the \u201cdot\u201d notation just like you do in VBScript. The only real difference here? We set the Visible property to <B>$<\/B>True, prefacing the built-in variable with a dollar sign. Remember when we said you had to preface variable names with a dollar sign? There&#8217;s a reason why we mentioned that.<\/P>\n<P>Come to think of it, yes, that just <I>might<\/I> be the first time we ever had a valid reason for saying something in this column.<\/P>\n<P>That brings us to the following two lines of code, lines where we use the <B>Add<\/B> method to add a new workbook to our instance of Excel, then use the <B>Item<\/B> property to create an object reference to the first worksheet in that workbook:<\/P><PRE class=\"codeSample\">$b = $a.Workbooks.Add()\n$c = $b.Worksheets.Item(1)\n<\/PRE>\n<P>Thanks for pointing that out: the second line <I>is<\/I> a tad bit different than what you\u2019d see in VBScript, isn\u2019t it? In VBScript we create an object reference to worksheet 1 using code similar to this:<\/P><PRE class=\"codeSample\">Set c = b.Worksheets(1)\n<\/PRE>\n<P>That\u2019s easy enough: we want worksheet 1 so we specify <B>Worksheets(1)<\/B>. Then why, in Windows PowerShell, do we refer to worksheet 1 as <B>Worksheets.Item(1)<\/B>?<\/P>\n<P>Well, as it turns out, the Item property is the default property for the Worksheet object. In VBScript you can usually get away without explicitly referencing the default property; if you don\u2019t specify a property name then VBScript uses the default property. That\u2019s not the case with Windows PowerShell, however. Suppose we left out the default property and tried creating an object reference using code like this:<\/P><PRE class=\"codeSample\">$c = $b.Worksheets(1)\n<\/PRE>\n<P>What&#8217;s going to happen in that case? This:<\/P><PRE class=\"codeSample\">Method invocation failed because [System.__ComObject] doesn&#8217;t contain a method named &#8216;Worksheets&#8217;.\n<\/PRE>\n<P>In other words, make sure you always include the property name, even when dealing with the default property.<\/P>\n<TABLE class=\"dataTable\" id=\"EXF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Interestingly enough, VBScript lets you do this any which way you want. Want to leave out the default property name? That&#8217;s fine. But what if you <I>want<\/I> to reference the default property name? Then go ahead and do so; this line of code works just fine in VBScript:<\/P><PRE class=\"codeSample\">Set c = b.Worksheets.Item(1)\n<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The next two lines of code are there just to show you how to actually do something with Excel once the application is up and running. In line 1, we add some text to cell A1 (row 1, column 1); again, notice that we explicitly referenc the <B>Item<\/B> property when dealing with the cells in the worksheet. After modifying cell A1 we then call the <B>SaveAs<\/B> method to save the spreadsheet as C:\\Scripts\\Test.xls:<\/P><PRE class=\"codeSample\">$c.Cells.Item(1,1) = &#8220;A value in cell A1.&#8221;\n$b.SaveAs(&#8220;C:\\Scripts\\Test.xls&#8221;)\n<\/PRE>\n<P>Speaking of methods, any time you call a method in Windows PowerShell you must include parentheses on the end, even if those parentheses are empty. For example, in VBScript you can call the Save method using code similar to this:<\/P><PRE class=\"codeSample\">b.Save\n<\/PRE>\n<P>That won\u2019t do you much good in Windows PowerShell, however. Instead, you need to tack on a set of parentheses, which tells Windows PowerShell that we\u2019re using a method:<\/P><PRE class=\"codeSample\">$b.Save()\n<\/PRE>\n<P>And, yes, we know. But you\u2019ll get used to it in no time.<\/P>\n<P>After we save the spreadsheet we call the <B>Quit<\/B> method (there\u2019s those parentheses again) and we&#8217;re done:<\/P><PRE class=\"codeSample\">$a.Quit()\n<\/PRE>\n<P>Hope that gets you started, MW. As for everyone else, if today\u2019s column seemed a bit mystifying, mainly because you\u2019ve never even <I>heard<\/I> of Windows PowerShell, well, you might want to take a peek at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\"><B>Windows PowerShell<\/B><\/A> center. Oh: and did we mention that Windows PowerShell Week is coming November 6-10, 2006? Well, we should have: undoubtedly those 5 webcasts will represent the best thing that\u2019s happened to Windows PowerShell since \u2026 well, Windows PowerShell is brand-new, so not a lot has actually happened to it yet. But you know what we mean.<\/P>\n<TABLE class=\"dataTable\" id=\"EFH\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Update: <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/webcasts\/ps.mspx\"><B>Windows PowerShell Week<\/B><\/A> is over, but you can watch the webcasts on-demand anytime.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept06\/hey0908.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept06\/hey0908.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to automate Microsoft Excel?&#8212; MW Hey, MW. Yes, it\u2019s true: the week of November 6-10, 2006 will be Windows PowerShell Week on TechNet, with the Scripting Guys presenting a series of webcasts (one each day, Monday through Friday) designed to introduce you to Windows PowerShell. Those [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[711,48,49,3,45],"class_list":["post-66513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to automate Microsoft Excel?&#8212; MW Hey, MW. Yes, it\u2019s true: the week of November 6-10, 2006 will be Windows PowerShell Week on TechNet, with the Scripting Guys presenting a series of webcasts (one each day, Monday through Friday) designed to introduce you to Windows PowerShell. Those [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66513","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66513"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66513\/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=66513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}