This morning I needed to find which scheduled task that ran a specific PowerShell script, and also which username that was configured to run the task. While the Get-ScheduledTask and Get-ScheduledTaskInfo cmdlets does retrieve detailed information about schedule tasks, they don't reveal the information about the username. Good old schtasks.exe to the rescue.
The Script
$TaskName = "*" # Change to look for specific scripts
$Action = "*Invoke-DPHealthCollectAndSummarization.ps1*" # Change to look for specific actions, or just * or *.ps1
schtasks.exe /Query /V /FO CSV |
ConvertFrom-Csv |
Where { $_.TaskName -ne "TaskName" -and $_.TaskName -like $TaskName -and $_.'Task To Run' -like $Action }|
Select-Object @{ label='Name';expression={split-path $_.TaskName -Leaf} }, Author ,'Run As User','Task To Run' |
Format-Table -Property * -AutoSize |
Out-String -Width 4096
Hi Johan. Recently I have written similar script. With following commands, in our company environment, it was possible to obtain the username:
Get-ScheduledTask | Select TaskName,Author -ExpandProperty Principal | Select TaskName, Author, UserId
Thank you!