0

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

TechFA
  • 129
  • 1
  • 9
  • The OP probably realized by now, but to anyone coming here from google, this is a classic example of what a lot (including me) of people go through when going to Powershell after a life using members of the Bourne Shell family. The person needs to go through a change in mindset to stop seeing things as text needing to be processed and starting to see things as objects with properties everywhere. – Doodad Mar 17 '21 at 19:14

1 Answers1

1

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]}}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • You're right, that calculated property is exactly what I needed. I did need to remove the first "DiskAllocation," you had in your answer. I was getting Select-Object : The property cannot be processed because the property "DiskAllocation" already exists. Once I took that out, it worked like a charm. Thanks for the help! – TechFA Apr 24 '20 at 13:16