Check Scheduled task for run as account PowerShell

Recently I needed to check for all scheduled task running on servers using a certain account as the password needed to be updated.  To search for these scheduled task I used schtask.exe

I wanted to integrate this with PowerShell so that I could do some connection tests, loop through a list of devices and output the results to CSV.  Below is the script I created it has three mandatory parameters for the computer list, run as account and export path.

## Set script parameter
[parameter(Mandatory = $true)]
[parameter(Mandatory = $true)]
[parameter(Mandatory = $true)]
## Get list of device to check
$comps = Get-Content $CompList

## Loop through each device
foreach ($comp in $Comps){
Write-Host “Testing connection to $($comp)” -ForegroundColor DarkGreen
$TC = Test-Connection $comp -Count 1 -ErrorAction SilentlyContinue

if ($TC){
Write-Host “Checkig $($comp)” -ForegroundColor Green

## Check scheduled task for specified run as account
$schtask = schtasks.exe /query /V /S $comp /FO CSV | ConvertFrom-Csv | Select-Object HostName,TaskName,Status,”Next Run Time”,”Run As User” |
Where-Object {$_.”Run As User” -contains $RunAsAccount}
if ($schtask){

## Export results
Write-Host “Task found exporting to results to $($ExportLocation)”
$schtask | Export-Csv “$ExportLocation\ScheduledTaskExport.csv” -NoTypeInformation -Append
else {
Write-Host “No task found with run as account”

else {
Write-Host “$($comp) not responding Exporting failures to log file located in $($ExportLocation)” -ForegroundColor Yellow
$comp | Out-File “$ExportLocation\FailureReport.log” -NoTypeInformation -Append

To run the script supple the three parameter like below. I used Check_RunAsAccount as the scrip name.

.\Check_RunAsAccount.ps1 -CompList D:\Scripts\Task_Scheduler\Complist.txt -RunAsAccount test1 -ExportLocation D:\Scripts\Task_Scheduler\

SCk1SCk4Once the script has completed there will be a result csv and if there are any non responding devices there will be a second csv.SCk5The export result will look like the below. SCk2