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
- 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.
- Secondly you tell the computer when to do it, the trigger, in this case upon logging in.
- Third you combine the what and when into a task variable.
- Lastly you register this task, giving it a name and supplying the task variable to the cmdlet.
No comments:
Post a Comment