1

I have the following piece of PowerShell 2.0 code that mostly works.

    #$UserProfilePath is the path to recursively search
    $MailBody = $MailBody + "`r`n`r`n Potential Valuable Files located in                 $NLEFriendlyName's profile include:`r`n`r`n"
    $FileList = get-childitem -path $UserProfilePath -Recurse
    ForEach ($File in $FileList) {
    if ($File.Extension -eq ".doc") {$UsefulFiles = $UsefulFiles + "`r`n" + $File.name}
    elseif ($File.Extension -eq ".docx") {$UsefulFiles = $UsefulFiles + "`r`n" + $File.name}
    elseif ($File.Extension -eq ".xls") {$UsefulFiles = $UsefulFiles + "`r`n" + $File.name}
    elseif ($File.Extension -eq ".xlsx") {$UsefulFiles = $UsefulFiles + "`r`n" +  $File.name}
    elseif ($File.Extension -eq ".ppt") {$UsefulFiles = $UsefulFiles + "`r`n" +  $File.name}
    elseif ($File.Extension -eq ".pptx") {$UsefulFiles = $UsefulFiles + "`r`n" +  $File.name}
    elseif ($File.Extension -eq ".pdf") {$UsefulFiles = $UsefulFiles + "`r`n" +  $File.name}
    elseif ($File.Extension -eq ".txt") {$UsefulFiles = $UsefulFiles + "`r`n" +  $File.name}
                            }
    $MailBody = $MailBody + $UsefulFiles

And it does mostly what I expect it to which is generating a list of files found in a user's profile directory.

The problem is that the carriage return (`r`n) isn't always respected. My $MailBody variable ends up looking like the below snippet where it starts out fine, but stops doing it.

20150114-Error.txt
Server List.txt
Server Port Breakdown.xlsx
ds_time.txt
Business Continuity - Call Center Services 2009a (2) (4)-old.pdf Business Continuity - Call Center Services 2009a (2) ....pdf New Text Document.txt Office2013Rearm.txt UserList.txt StopScreensaver.txt Testing Doc 201501081355.txt Antivirus KB.docx Antivirus-Deployment.txt

This posting somewhat alludes to a possibility, but I'm not sure on how to translate it to my script:

I'm sure there is a more optimal way to list all the file extensions as well.

Matt
  • 40,384
  • 7
  • 62
  • 97

1 Answers1

1

I tried to clean up the selection process a bit too.

#sample variables
#$UserProfilePath = 'C:\Users\bob'
#$NLEFriendlyName='bob'

#select extensions to search
$ext = ".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt"
$FileList = get-childitem -path $UserProfilePath -Recurse
$UsefulFiles = $FileList | Where-Object {$_.extension -in $ext} | select -expand name
$MailBody = $MailBody + "`r`n`r`n Potential Valuable Files located in $NLEFriendlyName's profile include:`r`n`r`n"
$MailBody = $MailBody + ($UsefulFiles -join "`r`n")
John Hubert
  • 2,174
  • 2
  • 16
  • 18
  • Thanks for pointing me in the right direction for the file extension array. I thought there would be an easier way then what I had initially. On the carriage return issue, it shows up beautiful if I view the mail body in something like GMail's web interface; however, if I vew it inside Outlook 2013, the carriage return doesn't seem to be consistently applied and I end up with what I posted in my original post. – Corporal Cupcake Apr 24 '15 at 14:42
  • In viewing the resulting email in Outlook Web Access, it appears great as well. Something peculiar with the way Outlook 2013 client deals with the `r`n text. I was not aware of the -join command so I get to do some reading. – Corporal Cupcake Apr 24 '15 at 14:55
  • I found a possible solution with the line break issue in Outlook 2013 client. I will have to try it out. http://stackoverflow.com/questions/136052/how-do-i-format-a-string-in-an-email-so-outlook-will-print-the-line-breaks – Corporal Cupcake Apr 24 '15 at 14:58
  • 1
    Based on the information on the above link to 136052, I added 5 extra spaces before the `r`n. It cleaned up my Outlook 2013 client mail body great. Thanks to John for the answers/assistance. – Corporal Cupcake Apr 24 '15 at 15:02
  • @CorporalCupcake Thanks for letting me know about the issue viewing the text in Outlook. I'll keep that in mind myself. Have fun learning PowerShell. – John Hubert Apr 24 '15 at 19:15