0

I'm developing code for monitoring memory that a specific program is using and then, if memory goes too high, it's gonna kill the process.

I'm wondering how I could remove extra blank spaces backward and forward the result, and then put the content into a variable.

Right now, the code is working, but it brings me some blank spaces. I know it's because of the Format-Table -Hide function, but I don't know how to fix it.

Eg.: With Chrome process.

$Pmemory = Get-Process -Name chrome |
           Group-Object -Property ProcessName |
           Select @{n='Memory';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1MB)}} |
           ft -hide |
           Out-String

$Lmemory = 1000

After all, I'm gonna compare both values, but my main question is how to remove those blank spaces.

2
  • 7
    "how to remove those blank spaces" By not having them in the first place. Replace ft -Hide | Out-String with Select-Object -Expand Memory. Only ever use Format-* cmdlet when you're presenting data to a user. NEVER when further processing of the data is intended/required. Commented Feb 28, 2019 at 19:26
  • Thank so much. It works! Commented Feb 28, 2019 at 19:31

1 Answer 1

0

You have to remove the Format-Table and Out-String from your code

Like this

$Pmemory = (Get-Process -Name chrome |
           Group-Object -Property ProcessName |
           Select @{n='Memory';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1MB)}}).Memory

$Lmemory = 1000

Output:

PS C:\Windows\system32> $Pmemory
1.968
Sign up to request clarification or add additional context in comments.

Comments

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.