Search code examples
excelpowershelltimestampappendxlsx

Add timestamp in Excel column using Powershell


I built a Powershell script to append XLSX source content to an XLSX target file. The target file is like this:

Date|Name|Node|Cluster
    |ABC PC   HS1
    |XZZ CC   HS2
    |XYZ DD   HS3

I want to add a timestamp to the first column for every row that gets appended. How can I achieve this?

$excel = Open-ExcelPackage "target.xlsx"
Import-Excel "source.xlsx" -WorksheetName "Node" | 
Export-Excel "target.xlsx" -WorksheetName "Node" -Append
Set-ExcelRow -ExcelPackage $excel -WorksheetName "Node" -Value {Get-Date (Get-Date).AddMonths(-1) -Format "MMM/yy"} -StartColumn 1 
Close-ExcelPackage -ExcelPackage $excel 

Solution

  • this should work:

    $date = Get-Date (Get-Date).AddMonths(-1) -Format "MMM/yy"
    $object = Import-Excel "source.xlsx" -WorksheetName "Node" 
    $object | Add-Member -NotePropertyName Date -NotePropertyValue $(Get-Date (Get-Date).AddMonths(-1) -Format "MMM/yy") 
    $object | Export-Excel "target.xlsx" -WorksheetName "Node" -Append