Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

Powershell - Excel Reports Automation (I)

This document discusses using PowerShell to automate the generation of Excel reports. It provides the basic PowerShell commands needed to open or create an Excel file, reference worksheets, assign values to cells, save the file, and close Excel. An example PowerShell script is given that creates a new Excel file, assigns values to 4 cells, saves the file, and closes Excel. The goal is to show how PowerShell can extract data from systems and use Excel to present it in a user-friendly report format through automated scripts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Powershell - Excel Reports Automation (I)

This document discusses using PowerShell to automate the generation of Excel reports. It provides the basic PowerShell commands needed to open or create an Excel file, reference worksheets, assign values to cells, save the file, and close Excel. An example PowerShell script is given that creates a new Excel file, assigns values to 4 cells, saves the file, and closes Excel. The goal is to show how PowerShell can extract data from systems and use Excel to present it in a user-friendly report format through automated scripts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Powershell – Excel reports automation

(I)
Posted on February 18, 2016

Powershell can be very useful to extract data from our systems.


Excel can be very useful to show that data in a friendly way.
Powershell + Excel = Automatic generation of useful reports

These are the basic powershell sentences and functions needed to start generating
our Excel reports using Powershell scripts:

Contents [hide]

1 Excel COM object


2 Make it visible or not:
3 Open or create new
4 Reference a worksheet
5 Assign values to cells
6 Save
7 Finish
8 Simple example script

Excel COM object

It is neccesary to reference a COM object to manipulate Excel documents

$EXCEL = New-Object -ComObject Excel.Application

Make it visible or not:

Visible is useful when creating the script for troubleshooting

$EXCEL.visible = $false

Open or create new

Open a preexisting excel:


$WORKBOOK = $EXCEL.workbooks.open("D:\MYEXCEL.xlsx")

Create new excel:

$WORKBOOK = $EXCEL.workbooks.Add()

Reference a worksheet

By index:

$WORKSHEETS = $WORKBOOK.worksheets
$WORKSHEET = $WORKBOOK.worksheets.Item(1)

By name:

$WORKSHEETS = $WORKBOOK.worksheets
$WORKSHEET = $WORKBOOK.worksheets.Item("MAILBOXES")

Assign values to cells

$WORKSHEET.cells.item(1,1)="Name 1"
$WORKSHEET.cells.item(1,2)="Value 1"
$WORKSHEET.cells.item(2,1)="Name 2"
$WORKSHEET.cells.item(2,2)="Value 2"

Save

$WORKBOOK.SaveAs("D:\MYEXCEL.xlsx")

Finish

$EXCEL.workbooks.Close()
$EXCEL.Application.Quit()

Simple example script

Example of script EXCEL.ps1 that shows the creation of a new excel document
(D:\MYNEWEXCEL.xlsx) and assings values to 4 cells:
$OUPUTFILE = "D:\MYNEWEXCEL.xlsx"
$EXCEL = New-Object -ComObject Excel.Application
$EXCEL.visible = $false

$WORKBOOK = $EXCEL.workbooks.Add()
$WORKSHEETS = $WORKBOOK.worksheets
$WORKSHEET = $WORKBOOK.worksheets.Item(1)

$WORKSHEET.cells.item(2,1)="Name 1"
$WORKSHEET.cells.item(2,2)="Value 1"
$WORKSHEET.cells.item(3,1)="Name 2"
$WORKSHEET.cells.item(3,2)="Value 2"

Write-Host "Saving $OUPUTFILE"


$WORKBOOK.SaveAs($OUPUTFILE)
Write-Host "Exiting..."
$EXCEL.workbooks.Close()
$EXCEL.Application.Quit()

Execution of the script:

Final result:

This is the first post related to basic Excel manipulation using Powershell. Please,
wait for the next

This entry was posted in Excel, Powershell and tagged automate, excel, howto,
powershell, tutorial by Sysadmin SomoIT. Bookmark the permalink
[https://somoit.net/powershell/powershell-excel-spreadsheet-mini-tutorial] .

You might also like