title: Powershell Email body variable
body: <p>I am having an issue with capturing the results of a variable in acceptable/readable format which are sent through an email. </p>
  
  <p>Following is the script I used,</p>
  
  <pre><code>$body = Search-ADAccount -UsersOnly -AccountExpired -SearchBase 'OU=Test1,OU=TEST,OU=Accounts,DC=home,DC=ac,DC=uk' | select SamAccountName, DistinguishedName, AccountExpirationDate
  
  $emailto = 'test@home.ac.uk'
  $emailfrom = 'AD-Accounts@home.ac.uk'
  $emailserver = '127.0.0.1'
  
  Send-MailMessage -To $emailto -From $emailfrom -Subject 'Expired Accounts' -Body $body -SmtpServer $emailserver 
  </code></pre>
  
  <p>$body variable output is exactly what I want to see in the email body.</p>
  
  <blockquote>
    <p>SamAccountName DistinguishedName                                                    AccountExpirationDate
    -------------- -----------------                                                    ---------------------
    jumartin       CN=user one,OU=test1,OU=Test,OU=Accounts,DC=home,DC=ac,DC=uk 19/10/2016 00:00:00<br>
    ricohvalue     CN=user two,OU=test1,OU=TEST,OU=Accounts,DC=home,DC=ac,DC=uk        02/09/2016 00:00:00   </p>
  </blockquote>
  
  <p>However the email comes out blank if there are more than one expired user and if there is one expired user than it comes out in the following format:-</p>
  
  <blockquote>
    <p>@{SamAccountName=userone; DistinguishedName=CN=user one,OU=test1,OU=test,OU=Accounts,DC=home,DC=ac,DC=uk; AccountExpirationDate=10/19/2016 00:00:00}</p>
  </blockquote>
  
  <p>All I am looking for is the SamAccountName, DistinguishedName and AccountExpirationDate in a nice line by line format in the body of email.</p>
  
  <p>i dont know how i would capture what i see on the screen within a email. Can someone tell me what am i doing wrong. </p>