Summary: Explore and discover the additional Windows PowerShell modules in Windows 8.1.
Honorary Scripting Guy, Sean Kearney, is here this week. Today, I'll wrap up our journey into the land of modules that are part of Windows 8.1. You also might enjoy reading:
- Windows 8.1 and the Land of Forgotten Modules: Part 1
- Windows 8.1 and the Land of Forgotten Modules: Part 2
- Windows 8.1 and the Land of Forgotten Modules: Part 3
- Windows 8.1 and the Land of Forgotten Modules: Part 4
Today I’m going to investigate a module that I find particularly useful. One I would have dived over the table for this in my last job. Today, it's the Scheduled Tasks module.
Let me grab the PowerShell can open…err…I mean, Get-Command, and crack open that module:
Get-Command –Module ScheduledTasks
Right off the bat I see New-ScheduledTask. Now THAT would have saved me so much when I was trying to do this on servers. Schtasks.exe worked; but again, it was yet another tool that had its own way of doing things in the land of CMD.exe.
But with Windows PowerShell—well, overall consistency is King in this world.
If I grab a little Help, I can easily see the process for building out a task in Windows 8.1—just as I can get Help with any Windows PowerShell cmdlet:
Get-Help New-ScheduledTask –Examples
There are quite a few pieces to set up, but I can easily see what pieces to modify to set up a task. It’s pretty obvious that the application name is Taskmgr.exe, so I can easily plug in Notepad.exe by changing the name in New-ScheduledTaskAction:
New-ScheduledTaskAction –Execute “Notepad.exe”
Although, why I would schedule to launch Notepad automatically is beyond me…personally, I would be launching One Note instead.
Because all of these are cmdlets,and getting Help is identical each time, if I need to see different ways to set up task triggers I would run Get-Help as normal:
Get-Help New-ScheduledTaskTrigger –Examples
Getting a list of tasks is now very easy in Windows PowerShell by using one cmdlet:
Get-ScheduledTask
Now I can actually do some cleanup and find only tasks that are Disabled (which makes me wonder what they’re doing on my computer right now):
Get-ScheduledTask | where { $_.State –eq ‘Disabled’ }
Or if prefer, I can find all tasks for DiskDiagnostic:
Get-ScheduledTask –TaskPath \Microsoft\Windows\DiskDiagnostic\
Not too many there and both of them disabled. I could remove them—but let’s do it the safe Windows PowerShell way. We’ll use –whatif first:
Get-ScheduledTask –TaskPath \Microsoft\Windows\DiskDiagnostic\ | Unregister-ScheduledTask –whatif
You didn’t think I was actually going to delete stock tasks in Windows did you? Of course not. But a cool thing about using Windows PowerShell is you can play with the live data.
Finally, the one thing I wish I had this for: A computer with the CPU maxed just right due to heavily scheduled and overlapped tasks.
My first challenge: Suspend EVERYTHING running.
Before Windows PowerShell, I had to manually poke through all of the scheduled tasks, find the ones that were running, then stop them one…by one…by one…
Then scream like a Monty Python Gumby, “Oh my brain! It hurts. It hurts!” What a slow way to do things.
However, we can now get all of those scheduled tasks by running one command:
$TaskList=Get-ScheduledTask | where { $_.State –eq ‘Running’ }
…and then immediately stop them:
$TaskList | Stop-ScheduledTask
…and then disable them to bring things under control:
$TaskList | Disable-ScheduledTask
When I’ve decided that it wasn’t any of my scheduled tasks, but some badly written SQL query somebody wrote with Select * and multiple cross-table joins doing a client side filter, I simple re-enable them and let the system go back to its normal happy level of work:
$TaskList | Enable-ScheduledTask
Are you also curious about the status of task? You can pipe a list of tasks to Get-ScheduledTaskInfo and find out how they’re doing. Let’s check out the task for Multimedia:
Get-ScheduledTask –TaskPath ‘\Microsoft\Windows\Multimedia\’ | Get-ScheduledTaskInfo
Or I can get the same information for all of the tasks that have not been “running so well” (as they say):
Get-ScheduledTask | Get-ScheduledTaskInfo | Where { $_.NumberOfMissedRuns –gt 0 } | Format-Table –Autosize
Pretty cool, eh? Why dig around for answers about your scheduled tasks when you can get Windows PowerShell to do the work for you?
That’s all we have in the Land of Forgotten Modules. Hopefully, there’s a few pieces in there you might find handy.
Remember, all of these modules are designed to also work in Windows Server 2012 R2…to make your life a little bit easier.
Check back to this channel regularly for more exciting Windows PowerShell goodness!
I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then remember eat your cmdlets each and every day with a taste dash of creativity.
Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy