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

**URL:** <https://forums.powershell.org/t/send-output-of-search-adaccount-as-body-variable-in-send-mailmessage/5843>\
**Category:** PowerShell Help\
**Created:** [February 23, 2016, 10:01am UTC](https://forums.powershell.org/t/send-output-of-search-adaccount-as-body-variable-in-send-mailmessage/5843 "2016-02-23T10:01:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocky-cabral1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rocky-cabral1/32/543_2.png) [@rocky-cabral1](https://forums.powershell.org/u/rocky-cabral1)\
**Post date:** [February 23, 2016, 10:01am UTC](https://forums.powershell.org/t/send-output-of-search-adaccount-as-body-variable-in-send-mailmessage/5843/1 "2016-02-23T10:01:20Z")

</div>

Hello,

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?

Script:  
#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

#Variables  
$from = "IT Dept. "  
$emailaddress = “[AdminGroup@company.com](mailto:AdminGroup@company.com)”  
$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

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 23, 2016, 10:12am UTC](https://forums.powershell.org/t/send-output-of-search-adaccount-as-body-variable-in-send-mailmessage/5843/2 "2016-02-23T10:12:11Z")

</div>

Closing this as a duplicate.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/send-output-of-search-adaccount-as-body-variable-in-send-mailmessage/5843/3 "2024-05-16T20:42:49Z")

</div>


