Remove Blank Space From Output in PowerShell

喜夏-厌秋 提交于 2021-01-29 14:34:12

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/54932772/remove-blank-space-from-output-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!