Ciprian
Ciprian

Reputation: 97

format txt file when sending powershell email

i have to send the output of a powershell script to outlook\external email and it looks really bad when arriving to destination. any ideas how to format it so it look as the original?

this is how my original .txt looks like:

some text here

Directory: C:\Users\xxx\Desktop

-a---         12/6/2013   2:31 PM      30823 diagram.docx
-a---        12/16/2013   2:17 PM       2002 Microsoft Outlook 2010.lnk
-a---        12/10/2013   2:25 PM       1576 new vlans.txt
-a---        12/12/2013   9:39 AM         59 pass.txt
-a---        12/11/2013   1:57 PM      14764 vlans for Q9.xlsx
-a---        12/10/2013   2:19 PM     182784 _Release Request Form V12.doc
Total size GB
--------------
0.450272973626852  

email:

enter image description here

this is the code i have used:

 'New-Item c:\users\xxx\desktop\pwr -type directory
echo "Good morning gentlemen, below are this week's  bakups report!" > c:\users\xxx\desktop\pwr\bkp.txt

'(Get-ChildItem -Path C:\Users\xxx\Desktop\*.*  | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-10) }) >> c:\users\xxx\desktop\pwr\bkp.txt

'(Get-ChildItem -Path C:\Users\xxx\Desktop\*.*  | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-10) }).Count > c:\users\xxx\desktop\pwr\tmp.txt

'Get-ChildItem c:\users\xxx\desktop -recurse | Measure-Object -property length -sum|ft @{Name="Total size GB "; Expression={$_.Sum / 1GB};align="left"} >> c:\users\xxx\desktop\pwr\bkp.txt 

'gwmi win32_volume -Filter 'drivetype = 3' | select driveletter, label, @{LABEL='GB freespace';EXPRESSION={$_.capacity / 1gb }} >> c:\users\xxx\desktop\pwr\tmp.txt
Get-Date -Format "dd/MM/yyyy" >> c:\users\xxx\desktop\pwr\tmp.txt


$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.Recipients.Add("[email protected]")  
$mail.Subject = "Weekly PME Backups $((get-date -format "dd/MM/yyyy"))" 
$Mail.Body = (Get-content "C:\Users\xxx\Desktop\pwr\bkp.txt")  
$Mail.Send()

thanks! Ciprian

Upvotes: 0

Views: 1480

Answers (1)

Ciprian
Ciprian

Reputation: 97

finally figured it out :) tested and working with outlook

$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.Recipients.Add("[email protected]")  
$mail.Subject = "Weekly  Backups $((get-date -format "dd/MM/yyyy"))" 
$Mail.Body = (Get-content "C:\Users\xxx\Desktop\pwr\bkp.txt")    
$Mail.body = $body -join "`n"
$Mail.Send()

Upvotes: 1

Related Questions