Tensigh
Tensigh

Reputation: 1050

How to replace text in output from format-list in Powershell

I run the following command to get a very basic audit of file permissions in PS:

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

The output of Format-List contains info in the path I want to strip out, namely Microsoft.PowerShell.Core\FileSystem::

But if I add {$_ -replace "Microsoft.PowerShell.Core\FileSystem::",""} before the output file pipe I get errors.

Is there a way to parse out this data through a piped output, or do I have to create a function?

Upvotes: 2

Views: 1438

Answers (2)

Avshalom
Avshalom

Reputation: 8889

You can use 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

Upvotes: 2

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58461

You could inline replace the contents of Format-List using a calculated property like this

@{Label="Path"; Expression={$_.path.Replace("Microsoft.PowerShell.Core\FileSystem::", "")}}

Your command then becomes

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

Upvotes: 3

Related Questions