I created a Powershell script to generate an Excel report for a client which is run by the task scheduler on Windows Server 2012 R2. When I executed the Powershell script manually the Excel file would save successfully and the Excel process exits. When the same script is ran using the task scheduler, the Excel process opens, Powershell script finishes, and the Excel process remains open forever and it will never save the file.

Below, you can find the code required to save an empty Excel sheet. Save it to any location you like and modify the path where the Excel file is saved if desired.

# Create the Excel object
$excel = New-Object -ComObject excel.application

# Add a workbook (required)
$workbook = $excel.Workbooks.Add()

# Save it
$workbook.SaveAs("C:\Users\<youruserhere>\Desktop\myexcel.xlsx")

# Quit
$excel.Quit()

# Release the COM object
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$excel) | Out-Null

Now let’s schedule the script.

  1. Open the Task Scheduler in Windows or Windows Server.
  2. Right-click ‘Task Scheduler (local)’ in the left pane.
  3. Select ‘Create Task’
  4. Give it a meaningful name.
  5. Select a user, preferably one that has permissions to save the Excel file to the path you entered in the script.
  6. Tick the radio button ‘Run whether the user is logged in or not’.
  7. In the ‘Triggers’ tab, enter the desired trigger.
  8. In the ‘Actions’ tab, make sure ‘Start a program’ is selected in the ‘Action’ combobox.
  9. Click the ‘Browse’ button and select the Powershell executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

  10. Then in the ‘Add arguments’ text box enter: -file “C:\path\to\your\script\myscript.ps1”

Now this is probably what you have done so far right? Now the last missing bit is to create these missing directories:

  • On a 32-bit and 64-bit operating system: C:\Windows\System32\config\systemprofile\Desktop

  • On a 64-bit operating system: C:\Windows\SysWOW64\config\systemprofile\Desktop