如何在Powershell中替换format-list中的输出中的文本

时间:2016-09-13 06:24:36

标签: powershell powershell-v3.0

我运行以下命令来对PS中的文件权限进行非常基本的审核:

Get-ChildItem -Directory E:\*,E:\*\*,E:\*\*\* | 
Get-Acl | Format-list Path,AccessToString | 
Out-File -Filepath c:\myfile.txt

Format-List的输出包含我要删除的路径中的信息,即Microsoft.PowerShell.Core\FileSystem::

但是如果我在输出文件管道之前添加{$_ -replace "Microsoft.PowerShell.Core\FileSystem::",""},我会收到错误。

有没有办法通过管道输出解析这些数据,还是我必须创建一个函数?

2 个答案:

答案 0 :(得分:2)

您可以使用像

这样的计算属性内联替换Format-List的内容
@{Label="Path"; Expression={$_.path.Replace("Microsoft.PowerShell.Core\FileSystem::", "")}}

然后你的命令变为

Get-ChildItem -Directory C:\* |
Get-acl |
Format-List @{Label="Path"; Expression={$_.path.Replace("Microsoft.PowerShell.Core\FileSystem::", "")}}, accesstostring |
Out-File -filepath c:\myfile.txt

答案 1 :(得分:2)

您可以使用Convert-Path

Get-ChildItem -Directory C:\*,C:\*\*,C:\*\*\* | 
Get-Acl | Format-list @{N="Path";E={Convert-Path $_.Path}},AccessToString | 
Out-File -Filepath c:\myfile.txt