Search This Blog

Sunday, December 17, 2023

PowerShell: Create scheduled task

Case where a scheduled task comes in handy

Lately I've been suffering from a explorer process that is not starting upon boot of the computer.

I log in to Windows and the screen is completely black, apart from the mouse pointer sometimes. I've had this issue on a couple of computers over the years, while it is nothing dangerous and easy to fix, it is still annoying.

The quick fix is easy, start task manager using Ctrl + Shift + Escape and run the new task "explorer.exe".

However, I wanted to explore the PowerShell way of perhaps creating a scheduled task that simply checks if the explorer is running, and if not, apply the quick fix automatically. Also I explored whether I could use .bat-file to run a PowerShell script that registers this scheduled task. Unfortunately, for a quick and safe fix it didn't work, you either have to write a more complicated workaround or run the bat file as an admin, which then introduced some other issues. The goal was to keep it very simple.

The PowerShell script that is a failsafe for the explorer process

The code for the fix script looks like this:

function Start-Explorer {

    Start-Process explorer.exe;

}

$Explorer = Get-Process -Name explorer -ErrorAction SilentlyContinue

if (-Not($Explorer)) {

    Start-Explorer;

}

Manually scheduling a task in taskschd.msc

It is completely fine to save this to a .ps1-file and then manually create a scheduled task. Simply start task scheduler, hit "new basic task" once you are in the main folder of tasks.

Trigger:
Being logging in.

Action:
Start a program.

Program/script:
powershell.exe

The added arguments:
-windowstyle hidden -executionpolicy bypass -scope currentuser "C:\temp\PathToScript.ps1"

Basically this tells the computer that upon login (by the user creating the task), run the specified script while bypassing execution policy and only for the user, this time.

Scheduling a task using PowerShell, the basics

This is the very basic method of a PowerShell script that is intended to register a task in the task scheduler. When I tried dot sourcing it, it failed to bypass the execution policy and when I dot sourced it as an admin it created a scheduled task for all users of the computer. This could probably be altered with more detailed parameters and a permanent fix of the execution policy, but it is beside the point of this post. The point of this segment is to show the basic mechanics of using PowerShell to schedule a task.

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-executionpolicy bypass -file `\"C:\Temp\Sample.ps1`\""
$trigger = New-ScheduledTaskTrigger -AtLogon
$task = New-ScheduledTask -Action $action -Trigger $trigger
Register-ScheduledTask NameOfTask -InputObject $task

As you can see it is a quite interesting and straightforward mechanic.

  1. The very first step is that you tell the computer what to do, in this case run a script with a few parameters, the extra symbols in the file path helps to character escape the extra quotes.
  2. Secondly you tell the computer when to do it, the trigger, in this case upon logging in.
  3. Third you combine the what and when into a task variable.
  4. Lastly you register this task, giving it a name and supplying the task variable to the cmdlet.
When working with PowerShell and the task scheduler, you can of course supply much more details, for example which user should be affected. However, from what I could see you are required to run this script as an admin if you want to run it from the console (dot sourcing).

Creating a .bat-file that runs a PowerShell script

As an added bonus and to wrap up the post of today, I will show you a simple way to use a bat file to kickstart a .ps1-file, with supplied parameters like execution policy bypass.

Open notepad, create a file with the following command:

powershell.exe -ExecutionPolicy Bypass -File .\ScriptInSameDirectory.ps1

You can add parameters like 
-nologo 
-windowstyle hidden 
-executionpolicy bypass and to specify current user add -scope currentuser

Note that if the .bat-file is not in the same folder as the target .ps1 you need to specify full path.

Good luck!

No comments:

Post a Comment