Send output of "Search-ADAccount" as body variable in SEND-MailMessage


I have a script that searches the user accounts that have not been logged on to in 30 days and email the results to a group.

The script works except that I’m using the OUT-STRING to a $BODY variable for the SEND-MailMessage and that sends it as a long string rather than a column list.

How can I send it as column list?

#Find users that have not logged on in 30 days
$a = Search-ADAccount -AccountInactive -UsersOnly -TimeSpan 30.00:00:00 -SearchBase “OU=Accounts,DC=HQ,DC=Company,DC=Com” | ? {$.DistinguishedName -notlike “OU=Services,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Shared,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Inactive,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Special,OU=Accounts,”}

$b = $a | select Name

$from = "IT Dept. "
$emailaddress = “
$body = Write-Output $b | Out-String
$smtpserver = “mail”

#Send Email Message
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body $body -BodyAsHtml -priority High

Closing this as a duplicate.