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.

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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 rn text. I was not aware of the -join command so I get to do some reading.
I found a possible solution with the line break issue in Outlook 2013 client. I will have to try it out. stackoverflow.com/questions/136052/…
Based on the information on the above link to 136052, I added 5 extra spaces before the rn. It cleaned up my Outlook 2013 client mail body great. Thanks to John for the answers/assistance.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.