如何将此 PowerShell 输出作为 Table 视图?

How to get this PowerShell output as a Table view?

我需要在 Table view.Also 中获取此 PowerShell 输出,需要在引号内。

当前输出格式:

Testing\Dump\DumpText-1.txt      Dump\DumpText-1.txt
Testing\Dump\DumpText-2.txt      Dump\DumpText-2.txt
Testing\Dump\SubDump1\DumpText-1.txt     SubDump1\DumpText-1.txt
Testing\Dump\SubDump1\DumpText-2.txt     SubDump1\DumpText-2.txt
Testing\Dump\SubDump2\Screenshot.png     SubDump2\Screenshot.png

要求的输出格式:

"Testing\Dump\DumpText-1.txt"            "Dump\DumpText-1.txt"
"Testing\Dump\DumpText-2.txt"            "Dump\DumpText-2.txt"
"Testing\Dump\SubDump1\DumpText-1.txt"   "SubDump1\DumpText-1.txt"
"Testing\Dump\SubDump1\DumpText-2.txt"   "SubDump1\DumpText-2.txt"
"Testing\Dump\SubDump2\Screenshot.png"   "SubDump2\Screenshot.png"

我的脚本是:

$directoryPath=$args[0]

Get-ChildItem $directoryPath -Recurse -Force | ForEach-Object -Process  { 
        
        if (!$_.PSIsContainer) {"$($_.FullName -creplace '^[^\]*\', '') `t` $($_.Directory.Name)$($_.Name)"}
    
    }

试试 PSCustomObject (and follow Quoting Rules):

Get-ChildItem $directoryPath -Recurse -Force -File |
    ForEach-Object -Process  { 
        [PSCustomObject]@{    
            fulln = """$($_.FullName -creplace '^[^\]*\', '')"""
            shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
        }
    }

编辑

要隐藏 table 中的列标题,请应用 Format-Table cmdlet as follows (read more at Controlling column widths with Format-Table):

Get-ChildItem $directoryPath -Recurse -Force -File |
    ForEach-Object -Process  { 
        [PSCustomObject]@{    
            fulln = """$($_.FullName -creplace '^[^\]*\', '')"""
            shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
        }
    } | Format-Table -HideTableHeaders -AutoSize

但是,Format- cmdlet 设计为仅用于console/screen 输出。在 Problem with Format- cmdlets

中阅读更多内容

高级脚本:

Param(
    [Parameter(Position=0, Mandatory=$false, ValueFromPipeline)]
    [string]$directoryPath='\bat\filez',
    [Parameter()]
    [switch]$AsObject
)

$outObj = Get-ChildItem $directoryPath -Recurse -Force -File |
    ForEach-Object -Process  { 
        [PSCustomObject]@{    
            fulln = """$($_.FullName -creplace '^[^\]*\', '')"""
            shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
        }
    }

if ( $AsObject.IsPresent ) {
    $outObj | Out-Default
} else {
    $outObj | Format-Table -HideTableHeaders -AutoSize
}

示例 1.\SO514630.ps1

"bat\filez\more_real.eml"      "filez\more_real.eml"
"bat\filez\PS_preferences.bat" "filez\PS_preferences.bat"
"bat\filez\Sample Input.eml"   "filez\Sample Input.eml"
"bat\filez\SampleInput.eml"    "filez\SampleInput.eml"
"bat\filez\folder\xxx.csv"     "folder\xxx.csv"

示例 2.\SO514630.ps1 \bat\foo.txt -AsObject

fulln                       shrtn
-----                       -----
"bat\files6711\foo.txt"  "676711\foo.txt"
"bat\files\bubu\foo.txt"    "bubu\foo.txt"
"bat\Unusual Names\foo.txt" "Unusual Names\foo.txt"
"bat\foo.txt"               "bat\foo.txt"