Tag Archives: PowerShell

Executing PowerShell scripts via Task Scheduler

I used excellent script Delete files older than x-days – Cleanup Script which requires bunch of parameters.

Sample command:

.\deleteold.ps1 -FolderPath "D:\test" -FileAge 7 -LogFile "D:\log.txt" -autolog -VerboseLog -CleanFolders

Most important parameters:

  • FolderPath
    The path that will be recursively scanned for old files.
  • Fileage
    Filter for age of file, entered in days. Use -1 for all files to be removed.
  • AutoLog
    Automatically generates file name at path specified in -logfile. If a file name is specified in the LogFile parameter and the AutoLog parameter is used only the path specified in LogFile is used. The file name is created with the following naming convention:

    Autolog_<FolderPath><dd-MM-yyyy_HHmm.ss>.log
  • VerboseLog
    Logs all delete operations to log, default behavior of the script is to log failed only.
  • CleanFolders
    If this switch is specified any empty folder will be removed. Default behavior of this script is to only delete folders that contained old files.

Creating a task in Task Scheduler

GENERAL
Just give it a name
scheduler01

TRIGGERS
Use schedule when you want to run it
scheduler02

ACTIONS

  • Program/script:

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

    Check what PowerShell version you have and update the path

  • Arguments:
    -Command "& d:\scripts\deleteold.ps1 -FolderPath "D:\test" -FileAge 0 -LogFile "D:\log.txt" -autolog -VerboseLog -CleanFolders; exit $LASTEXITCODE"

    exit $LASTEXITCODE is used to get exit code from the script
    O learnt this from Dmitry’s

scheduler03
scheduler04