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

1
  • 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. Commented Mar 17, 2021 at 19:14

1 Answer 1

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]}}
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.