Changing Excel Cells from PowerShell

by Jun 17, 2016

If you need to change the content of a specific cell in an Excel spreadsheet, take a look at this sample code:

$ExcelPath = 'c:\path..to..some..excel..file.xlst'
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Open($ExcelPath)

$workbook.ActiveSheet.Cells.Item(1,3) = 'I added this!'

Make sure you specify an existing Excel file. Or create a new one:

# create new Excel file
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Add()
$workbook.ActiveSheet.Cells.Item(1,3) = 'I added this!'
$ExcelPath = "$env:temp\newexcel.xlsx"
$workbook.SaveAs($ExcelPath)
$workbook.Close($false)
$excel.Quit()

# show new file in Explorer
explorer "/select,$ExcelPath"

Twitter This Tip! ReTweet this Tip!