在 Powershell 中的 Get-Child 项的输出行之间打印自定义字符串

Print cusrom strings between output lines of Get-Child items in Powershell

这里需要您的一点帮助。我正在尝试在 Powershell 中的 Get-ChildItem cmdlet 输出之间打印自定义字符串,但我不确定如何完成。

例如,我正在尝试查找其中包含关键字 "PostTestScript" 的所有文件,我正在执行以下操作。

Get-ChildItem | Select-String -Pattern "PostTestScript"

这是其生成的输出

aaa1.txt:31:    PostTestScript     = ''
aaa2.txt:31:    PostTestScript     = ''
aaa3.txt:31:    PostTestScript     = '' 

如果我想在如下文件名之间打印自定义输出(虚线),我该怎么办?

aaa1.txt:31:    PostTestScript     = ''
-------------------------------------------
aaa2.txt:31:    PostTestScript     = ''
-------------------------------------------
aaa3.txt:31:    PostTestScript     = '' 
-------------------------------------------

每行输出后的行分隔符

$pattern = "PostTestScript"
Get-ChildItem | Select-String -Pattern $pattern | 
    ForEach-Object {
        $_
        '-'*50       
    }

文件名更改时的行分隔符

$pattern = "PostTestScript"
$lastFile = [string]::Empty
Get-ChildItem | Select-String -Pattern $pattern | 
    ForEach-Object {
        if (($lastFile -ne [string]::Empty) -and ($_.FileName -ne $lastFile)) {
            '-'*50       
        }
        $lastFile = $_.FileName
        $_
    }
'='*50                   ### line separator at utter end (optional)

每个 $everyN 输出行后的行分隔符

$pattern = "PostTestScript"
$everyN = 4
$i = 0
Get-ChildItem | Select-String -Pattern $pattern | 
    ForEach-Object {
        if (($i -ne 0) -and ($i % $everyN) -eq 0) {
            '-'*50
        }
        $i++
        $lastFile = $_.FileName
        $_
    }
'='*50                   ### line separator at utter end (optional)