title: Powershell Excel Automation Save Workbook by Date
body: <p>I am creating a powershell script to populate an excel workbook with information from the "get-vm" command in SCVMM. </p>
  
  <p>The save filepath is a work in progress at the moment.
  I would like to run the script daily or weekly and save the generated excel workbook using that days date as a filename. Is this possible? How can I generate a date and use it as my filename for saving the excel output? any help would be great.</p>
  
  <pre><code>#run below line once and then comment out if not in VMM Command Shell. Will import modules for ISE/Powershell.
  #Import-Module "C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager"
  
  $server = Get-VMMServer -ComputerName "server"
  
  $vminfo = Get-VM -VMMServer $server 
  
  $xl=New-Object -ComObject "Excel.Application"
  $wb=$xl.Workbooks.Add()
  $ws=$wb.ActiveSheet
  $cells=$ws.Cells
  $xl.Visible=$True
  
  $cells.item(1,1)="{0} VMM Server Report" -f $server.Name
  $cells.item(1,1).font.bold=$True
  $cells.item(1,1).font.size=18
  
  #define some variables to control navigation
  $row=3
  $col=1
  
  #insert column headings
  "Name", "Description", "OperatingSystem", "CPUCount","Memory (GB)", "Status", "Hostname" | foreach {
  $cells.item($row,$col)=$_
  $cells.item($row,$col).font.bold=$True
  $col++
  }
  
  foreach ($vm in $vminfo) {
  $row++
  $col=1
  $cells.item($row,$col)=$vm.Name
  $col++
  $cells.item($row,$col)=$vm.Description
  $col++
  $cells.item($row,$col)=$vm.OperatingSystem.Name
  $col++
  $cells.item($row,$col)=$vm.CPUCount
  $col++
  $cells.item($row,$col)=$vm.Memory/1024
  $col++
  $cells.item($row,$col)=$vm.Status
  $col++
  $cells.item($row,$col)=$vm.HostName
  }
  $objRange = $ws.UsedRange 
  $objRange.EntireColumn.Autofit() 
  
  $date = get-date -DisplayHint date 
  
  $filepath="C:\Users\paulm\Documents\"
  
  if ($filepath) {
  $wb.SaveAs($filepath)
  
  }
  </code></pre>
  
  <p>Finished save script looks like this:</p>
  
  <pre><code>$date = Get-Date -Format yyyy-MM-dd 
  
  $wb.SaveAs("C:\Users\paulm\Documents\$date")+ ".xls"
  </code></pre>