Unable to format Powershell foreach output and attach it to email body and sub

I’m scheduling script if windows scheduled task is disabled from the given list then send email to me with Taskname in email subject and Taskinfo in body.

Email part is working fine

only I’m unable to format email body with disabled scheduler name and add disabled task name in to subject.

Please help me to format email body and add disabled task name to subject.

Here is my script-

$tasknamelist= Import-Csv "C:\Documents\task.csv"

foreach ($task in $tasknamelist) {
 $service=Get-ScheduledTask -TaskName "$taskname" | select -ExpandProperty State | Out-String
    if ($task.State -eq "Disabled") {
    $Body ="$service is not running"
else {

Write-Host "$Body Task is enabled" | Out-Null


} $Body
$From = "xxx@outlook.com"
$To = "xxx@outlook.com"
$Cc = "xxxx@outlook.com"
#$Attachment = "C:\temp\Some random file.txt"
$Subject = ""
$Body = "$Body"
$computer = $env:computername
$SMTPServer = "outlook.office365.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject "Task Scheduler is disabled on $computer" `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred


Try this…

$tasknamelist = Import-Csv "C:\Documents\task.csv"

$Body = @()

foreach ($task in $tasknamelist) {
    $service = Get-ScheduledTask -TaskName $task
    if ($service.State -eq "Disabled") {
        $Body += "$task is not running"

if ($Body)
    $From = "xxx@outlook.com"
    $To = "xxx@outlook.com"
    $Cc = "xxxx@outlook.com"
    #$Attachment = "C:\temp\Some random file.txt"
    $computer = $env:computername
    $Subject = "Task Scheduler is disabled on $computer"
    $SMTPServer = "outlook.office365.com"
    $SMTPPort = "587"
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject  $Subject`
    -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl `
    -Credential $cred

Thank you.

Thank you for the reply !!

getting error

Send-MailMessage : Cannot convert ‘System.Object[]’ to the type ‘System.String’ required by parameter ‘Body’. Specified method is not supported.

Can not convert system.object to system.string


$cred=(Get-Credential) #$tasknamelist = Import-Csv “C:\Users\kaargade\Documents\disabledtask.csv” $tasknamelist = Get-Content “C:\Users\kaargade\Documents\disabledtask.txt” $Body = @() foreach ($task in $tasknamelist) { $service = Get-ScheduledTask -TaskName $task if ($service.State -eq “Disabled”) { $Body += “$task is not running” } } if ($Body) { $From = “xx@outlook.com” $To = “xx@outlook.com” $Cc = “xx@outlook.com” #$Attachment = “C:\temp\Some random file.txt” $computer = $env:computername $Subject = “Task Scheduler is disabled on $computer” $SMTPServer = “outlook.office365.com” $SMTPPort = “587” Send-MailMessage -From $From -to $To -Cc $Cc -Subject "Task Scheduler is disabled on $computer"` -Body ($Body | Out-String) -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl ` -Credential $cred }

Thank you it’s working!! can you please help add disabled taskname to Email subject line, as body is fine.

Can you please elaborate a bit?

Thank you.

Thank you for reply!!


i want, if task is disabled from given list and condition true, email should trigger and in the email body, Task Name and Taskpath and task state should be added.

and in email subject line Task name should be added.

and if more than two task disabled, task name separated with comma or new line.


1)Task Name should be added to the Email subject, if multiple schedulers matches, name with commas added to subject.
2)and in the email body Disabled schedulers named added with new line.
3)In Body Task name and state and taskpath shold be added.


Okay good, where did you stuck? Did you try anything? In-fact by referring to the code above you can achieve what you want, please give it a try. Thank you.

I got it working subject part,

i only stuck with comma between two scheduler names. Or new line between to two schedulers name. In body and subject



Automatic-Device-Join Task

Scheduler is disabled on DESKTOP-


This is I’m getting currently in subject.

Alert:Recovery-Check Device-Sync Automatic-Device-Join Task Scheduler is disabled on DESKTOP-ARRSKL1

Is there any way to add comma or new. Line character in between two string in empty array.


[quote quote=195209]Is there any way to add comma or new. Line character in between two string in empty array.


After you added the required, you can join with -join, you can’t work with an empty array

[pre]$Body -join ‘,’ [/pre]

Thank you.

Thank you very much!!

i managed to format body with -BodyAsHtml parameter.

How ever i could not able implement your above solution.

$Body -Join ‘,’

please tell me where i have to -join.

i tried to join in if satement it’s bot working.

This can be simplified a bit. You have not mentioned how many tasks you are auditing or how many computers you are auditing. If it was 5 tasks and 10 computers, you would get up to 50 emails. The first suggestion is to send a single email per computer, so you could do something like this:

$creds = Get-Credential
$computer = $env:COMPUTERNAME

#Specific tasks names we are auditing
#$taskChk = Import-Csv -Path "C:\Documents\task.csv" | Select -ExpandProperty Name
$taskChk = 'UpdateAssistant',  'Recovery-Check'

$tasks = Get-ScheduledTask | Where{$_.State -eq 'Disabled' -and $taskChk -contains $_.TaskName}

if ($tasks) {
    $params = @{
        From       = "xxx@outlook.com"
        To         = "xxx@outlook.com"
        CC         = "xxxx@outlook.com"
        Subject    = "{0} tasks disabled on {1}" -f @($tasks).Count, $Computer
        SmtpServer = "outlook.office365.com"
        BodyAsHtml = $true
        Body       = ($tasks | ConvertTo-Html | Out-String)
        Port       = 587
        UseSsl     = $true
        Credential = $creds
    Send-MailMessage @params


This would give you a subject and body respectively:

PS C:\WINDOWS\system32> "{0} tasks disabled on {1}" -f @($tasks).Count, $Computer
2 tasks disabled on DESKTOP-ABC123

PS C:\WINDOWS\system32> $tasks

TaskPath                                       TaskName                          State     
--------                                       --------                          -----     
\Microsoft\Windows\UpdateAssistant\            UpdateAssistant                   Disabled  
\Microsoft\Windows\Workplace Join\             Recovery-Check                    Disabled  

This could also be a single email. You can loop through each computer, create a CimSession and remotely get the tasks. The goal would be getting an output like this:

TaskName           State PSComputerName
--------           ----- --------------
UpdateAssistant Disabled DESKTOP-ABC123
Recovery-Check  Disabled DESKTOP-ABC123
UpdateAssistant Disabled DESKTOP-ABC124
UpdateAssistant Disabled DESKTOP-ABC125
Recovery-Check  Disabled DESKTOP-ABC125

Now you would have all of the results in a single object which may even alleviate the need to send email.

I got everything, I wanted.

Thank very much for the help!!

Really appreciated!!