你如何修改PowerShell中Select-Object的输出?

huangapple go评论83阅读模式
英文:

How can I modify the output of Select-Object in PowerShell?

问题

我可以使用这段代码获取文件哈希值:

Get-ChildItem -Path "E:\Test 1" -Recurse -File | Get-FileHash -Algorithm MD5 | Select-Object Hash,Path | Format-Table -HideTableHeaders | Out-File -encoding ASCII -filepath "file.md5"

这给了我我想要的,除了我需要相对路径(相对于Get-ChilItem的路径,例如相对于E:\Test 1)。

我知道我可以用这个解析相对路径:

Get-ChildItem -path "E:\Test 1\*.*" -Recurse -File | Split-Path -NoQualifier

这个效果很好。

但我如何操纵 Split-Path -NoQualifier 部分以输出到第一个代码片段中的路径?最好是在可能的情况下用一行代码。

编辑:

只是想澄清,要输出相对于输入路径(即 "E:\Test 1")的路径。

归根结底,希望模仿Linux的输出,例如:find . -type f -exec md5sum {} +,其结果类似于:

md5hashmd5hashmd5hashmd5hashmd5h \path\to\file\file.ext

用户stackprotector的代码似乎有效!谢谢。

英文:

I can get file hashes using this code:

Get-ChildItem -Path "E:\Test 1" -Recurse -File | Get-FileHash -Algorithm MD5 | Select-Object Hash,Path | Format-Table -HideTableHeaders | Out-File -encoding ASCII -filepath "file.md5"

Which gives me exactly what I want, except I need relative paths (relative to the path for Get-ChilItem, e. g. relative to E:\Test 1).

I know I can resolve relative paths with this:

Get-ChildItem -path "E:\Test 1\*.*" -Recurse -File | Split-Path -NoQualifier

This works fine.

But how do I manipulate Split-Path -NoQualifier part to the Path output in the first code snippet? Preferably in a single line of code if possible.

EDIT:

Just wanted to clarify looking to output path RELATIVE to INPUT PATH (i.e. "E:\Test 1").

Bottom line looking to mimic output of Linux output from: find . -type f -exec md5sum {} + which results in something like this:

md5hashmd5hashmd5hashmd5hashmd5h \path\to\file\file.ext

Code from user stackprotector seems to work! Thanks.

答案1

得分: 1

Select-Object之后,你可以使用ForEach-Object修改每个元素。为了获得相对于你输入给Get-ChildItem的路径,你需要保留这个路径,可以通过使用一个变量($basepath)来实现。这是如何生成相对路径的方式:

ForEach-Object {$_.Path = ($_.Path -replace [regex]::Escape($basepath), '').trimstart('\'); $_}

因此,总体上的代码如下:

$basepath = 'E:\Test 1'
Get-ChildItem -Path $basepath -Recurse -File | Get-FileHash -Algorithm MD5 | Select-Object Hash,Path | ForEach-Object {$_.Path = ($_.Path -replace [regex]::Escape($basepath), '').trimstart('\'); $_} | Format-Table -HideTableHeaders | Out-File -encoding ASCII -filepath "file.md5"
英文:

You can modify each element after Select-Object with ForEach-Object. To get paths relative to what you input to Get-ChildItem, you have to preserve that somehow, e. g. by using a variable ($basepath). This is how you can generate your relative path:

ForEach-Object {$_.Path = ($_.Path -replace [regex]::Escape($basepath), '').trimstart('\'); $_}

So in total:

$basepath = 'E:\Test 1'
Get-ChildItem -Path $basepath -Recurse -File | Get-FileHash -Algorithm MD5 | Select-Object Hash,Path | ForEach-Object {$_.Path = ($_.Path -replace [regex]::Escape($basepath), '').trimstart('\'); $_} | Format-Table -HideTableHeaders | Out-File -encoding ASCII -filepath "file.md5"

huangapple
  • 本文由 发表于 2023年5月25日 10:47:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定