Monday, March 19, 2018

Format Table Columns

Earlier, we saw how to retrieve processes sorted by CPU:

Get-Process | Sort-Object CPU -Descending


Then we winnowed down the list to just the top 10 processes taking CPU cycles:
  
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10


Next we stripped out extraneous detail and just focused on the process name and the CPU using Format-Table:

Get-Process | Sort-Object CPU -Descending | Select-Object `
 -First 10 | Format-Table Name, CPU -Autosize





So it looks like PowerShell calculates out to seven decimal places. Er, millionths of a second? I'm not sure that much precision is necessary. Any way to clean that up?

Sure, using a calculated field:

Get-Process | Sort-Object CPU -Descending | Select-Object `
 -First 10 | Format-Table Name,@{Name="CPU(Min)";Expression` ={"{0:F2}" -f($_.CPU/60)}} -Autosize


 


A calculated field starts with the '@' symbol followed by curly brackets { }. 'Name="CPU(Min)" is what the column header will be labeled, as in "Minutes of CPU time". The Expression is the calculation to apply to the data in that column. In this case, we took the CPU cycles in seconds, divided them by sixty to just get the minutes, and only specified two decimal places (the 'F2' portion.)  How does that look?

 


Much nicer.

Saturday, March 17, 2018

Format a Table

In this previous post, we used PowerShell to retrieve processes sorted by CPU:

Get-Process | Sort-Object CPU -Descending




And in this previous post, we winnowed down the list to just the top 10 processes taking CPU cycles:

Get-Process | Sort-Object CPU -Descending | Select-Object -First 10

 












  

But that might get us more information than we really need. Handles? PM(K)? Is there a way to just display the process name and the CPU, while leaving out the extraneous data?

Sure. Just use Format-Table:

Get-Process | Sort-Object CPU -Descending | Select-Object `
 -First 10 | Format-Table Name, CPU -Autosize




We just pipe our data stream to Format-Table and identify which columns we want displayed. 

The -Autosize parameter lines up the columns neatly together. Try the command without the -Autosize parameter to see the difference. 


Friday, March 9, 2018

Select Objects from a List

In this previous post, we saw how to use PowerShell to find our processes, sorted by CPU:

 Get-Process | Sort-Object CPU -Descending


But most computers have a lot of processes. What if we wanted to know, say, the top 10 processes consuming CPU cycles? Well then, we can funnel our results through another pipeline into the cmdlet Select-Object:

 Get-Process | Sort-Object CPU -Descending | Select-Object -First 10

 

 

Friday, March 2, 2018

Find High-CPU Processes with PowerShell

It's easy to find the processes on a computer with PowerShell by calling Get-Process:

Get-Process | Sort-Object CPU -Descending




This command gets the computer's running processes, pipes them into Sort-Object, which displays the processes using the most CPU in Descending order.