Petri

Integrating Microsoft Excel with PowerShell: Build a Basic Report

Not too long ago I wrote about Integrating Microsoft Word with PowerShell, but I know that after text files, the most used tool for Windows administration is Microsoft Excel. Based on what I see in forums and talking with people at conferences, there is a lot of information tucked away in Excel spreadsheets. There’s no shortage of people who want to save information to Excel or read from it. Normally, I tell people that if they are using PowerShell, they can simply export results to a CSV file and then open that in Excel, but perhaps you really need an Excel file.

I’m going to take the next few columns and explore how to integrate Microsoft Excel with Windows PowerShell. Today we’ll go into building a basic report with Microsoft Excel and Windows PowerShell. In part two, I’ll go over how to create a richer Excel document. Later, in part three I’ll show you how to read data from an Excel file.

Sponsored

How to Integrate Microsoft Excel with PowerShell

Microsoft Excel has a very complex COM object model that we can interact with in Windows PowerShell. Let’s start at the beginning and create an object for the Excel application.

Right now Excel is running in the background, even though there isn’t an interactive window.

Next, we’ll create a workbook object.

Sponsored

And from here, we’ll create a worksheet object.

You can pipe any of these objects to Get-Member to learn more about them. For now, let’s make the application visible.

When you get to scripting you don’t need this, but it will help to see the results from our PowerShell commands. There are a variety of ways to enter information into the spreadsheet. For simple tasks, I’m going to show how to do this using the cell objects.

Each cell object can be retrieved as an item using row and column coordinates.

If you try this, you’ll get a lot of information. We’ll go ahead and enter some information into the first cell.

Your computer name should now be in cell A1. Let’s add a bit more data.

It’s really that easy. You just need to keep track of where you are. If you need some basic formatting, you can use each cell’s Font property.

When it is time, we can save the file using the SaveAs() method from the WorkBook object.

To fully exit, we’ll close the workbook and quit Excel.

If you check processes, you might still see Excel running but it should exit within 5 to 10 minutes, at least based on my experience. Those are the basic nuts and bolts, but before we wrap up, let me leave you with a sample script you can download from the site to pull all of this material together.

This is something you might want to do in PowerShell: use WMI to get disk usage information and record it into an Excel spreadsheet. The script takes a computername as a parameter but defaults to the localhost. It then uses Get-WMIObject to retrieve disk information.

Sponsored

The first part of the script should look familiar, as it creates the Excel application and objects. The script inserts a title in A1.

The main part of the script needs to take the data for each logical disk and write a few properties to Excel. Because I’m going to use cell objects with references to rows and columns, I’ll define some variables to help with navigation.

With these, I can insert my table headings.

Each time through the loop, $col is increased by one so I keep “moving” to the right. Now I need to iterate through the collection of disks. Each time through, I need to move “down” one row and make sure I start at the first column.

I then insert the appropriate WMI property into the appropriate cell. By incrementing the column, I can “move” to the left and continue the process. Notice I’m also formatting the value of each cell using the NumberFormat property. One way to discover things like this is to record an Excel macro making all the changes you want, and then look at the resulting VBA code. With a little practice, you can translate the commands into PowerShell.

When the script has finished writing the data to the spreadsheet, I make it visible and prompt the user for a file name. If one is entered, then the file is saved. Otherwise, you can adjust the spreadsheet further and then save it manually. This demo script does not automatically close Excel. The script should produce something like Figure 1.

PowerShell export to Excel

Figure 1: A PowerShell Generated Excel Report

Conclusion

I know this is a lot to tackle, so I’ll wrap it up for now. In Part 2, we’ll look at some additional formatting options and other items that take advantage of Microsoft Excel. If you are going to create Excel documents from PowerShell, you might as well make the most of it.

Sponsored

Join The Conversation

Sponsors

Sponsors