特定类型和包含在字符串中的Powershell递归文件列表

特定类型和包含在字符串中的Powershell递归文件列表,powershell,file-type,Powershell,File Type,我有下面一段PowerShell 2.0代码,基本上可以正常工作 #$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-childi

我有下面一段PowerShell 2.0代码,基本上可以正常工作

    #$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
它主要完成我所期望的功能,即生成在用户的配置文件目录中找到的文件列表

问题是回车
(`r`n)
并不总是得到尊重。我的
$MailBody
变量最终看起来像下面的代码段,开始时很好,但停止了

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
有点暗示了一种可能性,但我不确定如何将其翻译成我的脚本:


我相信还有一种更为理想的方式列出所有文件扩展名。

我也尝试过清理一下选择过程

#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")

感谢您为我指出了文件扩展名数组的正确方向。我认为会有一个比我最初拥有的更简单的方法。在回车问题上,如果我在GMail的web界面中查看邮件正文,它会显得很漂亮;但是,如果我在Outlook 2013中查看它,回车符似乎没有得到一致的应用,我最终得到了我在原始帖子中发布的内容。在Outlook Web Access中查看生成的电子邮件时,它看起来也很棒。Outlook 2013客户端处理
r
n文本的方式有些特殊。我不知道-join命令,所以我开始阅读。我在Outlook 2013客户端中找到了一个可能的解决方案,解决了换行问题。我得试试看。根据上面链接到136052的信息,我在
r
n之前添加了5个额外的空格。它清理了我的Outlook 2013客户端邮件正文非常好。感谢John的回答/帮助。@CorporatalCupcake感谢您让我知道在Outlook中查看文本的问题。我自己会记住的。享受学习PowerShell的乐趣。