Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Quick and Dirty Excel from PowerShell

Posted on May 10, 2012

I continue to tinker with Office applications and Windows PowerShell. I was looking at an Excel issue related to opening a new workbook. To verify the problem wasn't PowerShell related I offered a suggestion to try creating an Excel workbook in VBScript.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

In VBScript creating a blank workbook in Microsoft Excel can be accomplished with three lines of code.


set xl=wscript.createObject("Excel.Application")
set wb=xl.workbooks.add()
xl.Visible=vbTrue

In Windows PowerShell we essentially use the same 3 lines.


$xl=new-object -com "excel.application"
$sb=$xl.Workbooks.Add()
$xl.Visible=$True

But PowerShell is all about the objects, so how about a quick and dirty approach? Something perhaps in one line:


PS S:\> ((new-object -com "excel.application").Workbooks.Add()).application.Visible=$True

You could easily build a simple function to run the 3 lines it takes to open a new Excel workbook but one-liners like this make me smile. Granted, I'm probably not likely to use it, but if nothing else I think it reinforces the object nature of Windows PowerShell.

Each expression nested in () is treated as an object. The inner most expression using New-Object, creates the Excel application object which has a Workbooks property that is an object with an Add() method. Invoking this method creates the workbook object. If I stopped there Excel would be running but not visible. So I get the Application property of the workbook object, and set the Visible property to True.

This command doesn't offer any way to continue working with Excel from PowerShell; it only opens a new workbook. Let me be clear, I'm not advocating that this is how you work with Excel from PowerShell. I think you will need scripts like the ones I posted from my Deep Dive session. But this one-liner demonstrates what is possible with PowerShell and maybe it will add a little smile to your day as well.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Quick and Dirty Excel from PowerShell”

  1. Arnaud Petitjean says:
    May 11, 2012 at 12:05 am

    Hey Jeff!

    As weird as it is, the vbscript version works fine whereas I still have the same bug with PowerShell…. 🙁

    -Arnaud

    1. Jeffery Hicks says:
      May 11, 2012 at 7:11 am

      The only other thing I can think of, and it shouldn’t make a difference but maybe I’m wrong, is to do a repair install of Office and make sure you have installed .NET language support. PowerShell is really still using the Interop class to work with COM. But there may be some disconnect because of the language.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d