Showing posts with label Sort-Object. Show all posts
Showing posts with label Sort-Object. Show all posts

Thursday, May 17, 2018

Choose Random Numbers with PowerShell

Do we need to choose a number from 1 to 100?

Do we need to choose five random numbers from 1 to 100?

Do we have PowerShell?

Then we are all set.

First, create an array of numbers from 1 to 100:

$array = (1..100)

Then let PowerShell randomly choose 5 numbers from the array:

Get-Random -InputObject $array -Count 5




And if we need those results in numerical order?

Get-Random -InputObject $array -Count 5 | Sort-Object



 
   

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.