gpt4 book ai didi

powershell - 获取 For-Each 数组的结果并显示在表格中,其中列标题每个结果一行

转载 作者:行者123 更新时间:2023-12-03 16:38:01 25 4
gpt4 key购买 nike

我正在尝试获取文件列表以及显示在由两列(名称和行数)组成的表格中的每个文件的行数。

我尝试过使用格式表,但我认为问题不在于表的格式,而更多地与我的结果是单独的结果有关。见下文

#Get a list of files in the filepath location
$files = Get-ChildItem $filepath

$files | ForEach-Object { $_ ; $_ | Get-Content | Measure-Object -Line} | Format-Table Name,Lines

预期结果

名称行

文件A9

文件B89

实际结果

名称行

文件A
9
文件B
89

最佳答案

另一种方法如何制作这样的自定义对象:Using PowerShell's Calculated Properties :

$files | Select-Object -Property @{ N = 'Name' ; E = { $_.Name} }, 
@{ N = 'Lines'; E = { ($_ | Get-Content | Measure-Object -Line).Lines } }
Name                      Lines
---- -----
dotNetEnumClass.ps1 232
DotNetVersions.ps1 9
dotNETversionTable.ps1 64

关于powershell - 获取 For-Each 数组的结果并显示在表格中,其中列标题每个结果一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57442853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com