Hostwinds Blog

Search results for:


The Power of PowerShell Featured Image

The Power of PowerShell

by: Karlito Bonnevie  /  April 29, 2022


When comparing the commands of the traditional Windows command-line interface (CLI) with those of a Linux CLI, there's really no comparison. There is nothing in the traditional Windows CLI (cd, dir, copy, del, etc.) as powerful as awk, sed, head, tail, tsort, uniq, split, etc. That said, one of the common issues with the Linux CLI is that text stream processing is often required to get the desired result. For example, say you'd like the IPv6 address of your Linux machine. One approach might be:

ifconfig | grep inet6 | head -1 | awk '{print $2}'

The grep command grabs multiple lines containing "inet6", the head command grabs the first of these, and awk prints the second field (i.e., the second string) in the line, producing the desired IPv6 value. Pretty slick really but we're processing text streams, which can quickly become quite complicated. For example, say you'd like to do the following:

  • List all system processes, then
  • Sort the list in descending order by handle count followed by CPU usage time, then
  • View the list as a table showing process name, handle count, CPU time, and full process file path?

The PowerShell to do this is fairly straightforward:

Get-Process | Sort-Object -Property HandleCount, CPU | Format-Table -Property Name, HandleCount, CPU, Path

And here's a snippet of the output. As can be seen, the table is sorted by handle count, then by CPU time (note the descending CPU times for the three processes using 18 handles):

Name                    HandleCount        CPU Path
----                    -----------        --- ----
chrome                          165     375.13 /opt/google/chrome/chrome
pwsh                            128       81.1 /opt/microsoft/powershell/7/pwsh
chrome                           52     216.36 /opt/google/chrome/chrome
chrome                           44     151.13 /opt/google/chrome/chrome
Xvnc                             33     254.66 /usr/bin/Xvnc
dbus-daemon                      31       0.23 /usr/bin/dbus-daemon
chrome                           23       0.45 /opt/google/chrome/chrome
xfce4-terminal                   18      16.39 /usr/bin/xfce4-terminal
xfce4-session                    18       0.12 /usr/bin/xfce4-session
xrdp-chansrv                     18       0.05 /usr/sbin/xrdp-chansrv
xfdesktop                        15       4.39 /usr/bin/xfdesktop
xfce4-power-manager              15       0.39 /usr/bin/xfce4-power-manager
chrome                           15       0.03 /opt/google/chrome/chrome

Now say that in addition to the above, you'd like to group by handle count? It couldn't get much easier, just add the -GroupBy HandleCount argument to the Format-Table cmdlet (yes, that's what they're called):

Get-Process | Sort-Object -Property HandleCount, CPU | Format-Table -Property Name, HandleCount, CPU, Path -GroupBy HandleCount

Here's a snippet of that output:

   HandleCount: 18

Name           HandleCount   CPU Path
----           -----------   --- ----
xfce4-terminal          18 16.68 /usr/bin/xfce4-terminal
xfce4-session           18  0.12 /usr/bin/xfce4-session
xrdp-chansrv            18  0.05 /usr/sbin/xrdp-chansrv

   HandleCount: 15

Name                HandleCount  CPU Path
----                -----------  --- ----
xfdesktop                    15 4.43 /usr/bin/xfdesktop
xfce4-power-manager          15  0.4 /usr/bin/xfce4-power-manager
chrome                       15 0.03 /opt/google/chrome/chrome

Notice that there's not a hint of text stream processing in any of these PowerShell examples. That's because PowerShell passes objects (with various properties) between cmdlets and not data streams. If you really wanted to, you can still manipulate text streams with the Select-String cmdlet but there's rarely a need to do so. By the way, in the last example, you could group by any Get-Process object property and you can use Get-Process | Get-Member to see quickly see what properties are available.

Sound intriguing? Wondering how to get started? Then see Using PowerShell with Linux.

Written by Karlito Bonnevie  /  April 29, 2022