POWERSHELL: Get List Of Scheduled Tasks In Properly Formatted CSV
Just when the Powershell Kid thought that CSV was a great way of getting outputs, CSV betrays him.
He realizes this when he tries to create a CSV file that lists all the Scheduled tasks on his computer. He runs this small command which is supposed to get him an output in CSV without any hustle, and it does get him an output in CSV file.
schtasks /query /FO CSV > "C:\Users\PK\TaskSchedList.csv"
But wait!!. It opens in MS Excel fine, but everything in the file is in a “single” column. This is not how he wanted it. Well, of course, it has a way around, by doing some MS Excel Mumbo Jumbo, but he doesn’t like this way of doing it. So he tries this new hack. Powershell does have its ways to get things done.
#Save this as a file named "TaskSchedList.ps1"
$Scriptpath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
$tempfile = "$($env:temp)TempTaskSchedList.csv"
$outputFile = "$ScriptpathTaskSchedList.csv" schtasks /query /FO CSV > $tempfile
Import-csv -Path $tempfile | ?{
$_.Taskname -notmatch "TaskName"
} | Export-csv -Path $outputFile -NoTypeInformation
And great!! It does wonders!
Subscribe to my newsletter
Read articles from Anupam Majhi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anupam Majhi
Anupam Majhi
A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.