Table of Contents


The Requirement









To create a periodic action that reports in CSV the top 20 processes into a folder in Desktop, every hour.

We have been doing a lot of PowerShell Scripting in the last years. So we have to work with Task Scheduler a lot in recent days. Let's show the configuration.

    List of steps:

  1. Create our PowerShell script.

    Let's use an advanced PowerShell liner script for this task:

    
    
    <pre class="brush:powershell">    
    $start= Get-date
    $file = $start.Tostring("yyyyMMdd-hhmmss")
    Get-Process -IncludeUserName * | select @{l="Date";e={$($start)}},ProcessName,CPU,Id,StartTime,Username | Sort-Object CPU -Descending | select -First 10 | ConvertTo-Csv -NoTypeInformation  | Out-File ".\$file.csv"
    </pre>








    This script will get all the processes on the computer when running it and it will add the 'Date' where we are doing the query. Select some variables of interest and then sort descending using the "WorkingSet" property. Select the first 20, convert the object to CSV (no type information about the class) and finally, the output is the Output.CSV file (in the same folder of running the script).

  2. Configure the task.







    Open Task Manager by clicking the Windows icon, and type "task scheduler".

















    Once open, create a Task by clicking the "Create Task" link in the "Actions section".























    At the start, we are located in the "General" tab. On the next screen add a name and make sure that the checkbox "Run it with the highest privileges" is checked.








    Move to the Triggers tab. Here we configure that it should execute every hour. To do so, we need to click the "New" Button and then set as shown in the next image. Click OK.








    The "Actions" tab is the important one. We click on "New" on the program script and add C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe (without quotes since there's no space in there).















    In the arguments is the path of the file. If it contains spaces, add quotes: -file "C:\Users\j0rt3g4\Desktop\scripts\Startcoco.ps1"















    In the "Start in"  is a path, and it should never contain any blank space. For this example, the path of the reports would be: C:\Users\j0rt3g4\Desktop\scripts























    Finally, the settings should be seen like this. The final configuration tab is called "Settings." In here we just need to check that "Allow task to be run on demand" and "Do not start a new instance" are checked and selected respectively.








  3. Make sure that it's working or troubleshoot what we could have missed.







    Enable All Tasks History for all tasks.









    Make sure that "start in, contains the file".







    Successful code is 0x000000.







    And it should be generating every single hour. In the "start in" path0 configured in the "Action" of the task.







    At the end of our "Start in" we should see something like this:























Thank you so much for reading.