Submitted By: Kent Finkle

Creates a new spreadsheet featuring a file name based on the current date. Designed to be run each night as a scheduled task.

PowerShell
Edit|Remove
$comments = @'
Script name: Create-DatedXls.ps1
Created on: Monday, July 02, 2007
Author: Kent Finkle
Purpose: How can I use Windows Powershell to
Create a New Excel Spreadsheet at Midnight Each Night?
'@
#-----------------------------------------------------
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers() 
}
#-----------------------------------------------------
 
$m = (get-date).month

$d = (get-date).day

$y = [string] (get-date).year 
$y = $y.substring($y.length - 2, 2)
 
$f = "C:\Scripts\" + $m + "-" + $d + "-" + $y + ".xls"
 
$xl = new-object -comobject excel.application
$xl.Visible = $True
 
$wb = $xl.Workbooks.Add()
$a = $wb.SaveAs($f)
 
$a = $xl.Quit()
 
$a = Release-Ref($wb)
$a = Release-Ref($xl)