Showing posts with label Get-Process. Show all posts
Showing posts with label Get-Process. Show all posts

Tuesday, May 15, 2018

PowerShell: Forcing Confirmation

In a previous post, we saw how to check the Confirmation Preference, and how it works compare to a cmdlet's built-in confirmation level. But if we want to force a cmdlet to run with confirmation, we can use the '-Confirm' switch.

Here's how that works:

 

Like last time, we launched an instance of calculator.exe, found the process ID using Get-Process, then killed it with Stop-Process. PowerShell didn't hesitate to do as we commanded. But for safety's sake, let's add the '-Confirm' switch:



This time we had to take an extra step before the process was stopped. This feature would be useful in, for example, an interactive script designed for newer users, who might not be as comfortable taking permanent or destructive actions.

Tuesday, April 3, 2018

Turn Complex Commands into Functions or Scripts

We've been crafting a complicated but powerful command to get the top ten CPU-eating processes on a computer, presented in a nice readable format:

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



 

But who wants to type that ugly command repeatedly, or can even remember it from one day to the next? 

Fortunately, PowerShell makes it easy to execute it using two methods. The first is to wrap the long command inside a function:

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




Run all that in your PowerShell window, and thereafter we can get the information by simply typing:

Get-TopCPU

 

There. That was easy.

Unfortunately, the function will be lost as soon as we close the PowerShell window. If we want to keep a custom function permanently, we can add it to our PowerShell profile. (Enter 'notepad $profile' for a quick edit.)

Another way to keep the long ugly command is to enclose the command in quotes and redirect it to a script:

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


Then, for as long as we have the script saved somewhere convenient, we can always launch it:


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.