regexpowershellreplaceselect-objectwrite-host

Powershell Modify Select-Object Property Before Output


How would I go about removing the leading "{Storage consumed:, " and the trailing "}" from the DiskAllocation property of this Select-Object so that only the size remains?

For Example: {Storage consumed:, 48.62 MB} becomes 48.62 MB

I know that I need to do some sort of replace statement with regex, but I am a beginner with Powershell. Any help would be appreciated.

$DS | Select-Object -Property Computer, Name, ObjectType, DiskAllocation | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Cyan

The output currently looks like this:

Output


Solution

  • Judging from the output, the DiskAllocation property holds a two-item array - you can select only the second item with a calculated property:

    Select-Object -Property Computer, Name, ObjectType, DiskAllocation, @{Name='DiskAllocation';Expression={$_.DiskAllocation[1]}}