PowerShell Survival Guide Part 2

Marc Kirk
4 min readOct 22, 2022

--

Lots of Versions of PowerShell

Windows ships with PowerShell 5.1 as standard. 5.1 is shit, so you’ll want to upgrade to the latest and greatest — version 7 — which is less shit.

Naturally, in the world of IT, we are consumed by bigger numbers. Bigger is better — generally speaking. Version 7.* is open-source and runs on MacOS and Linux. However, the MacOS and Linux versions are vastly inferior to the Windows versions and contain considerably less cmdlets.

Cmdlet

A cmdlet — pronounced command-let — are commands on steroids! Cmdlets are user friendly and reduce our need to memorize.

Cmdlets are a quantum-leap in command-line usability. Compared to PowerShell, the old way of doing things was frustrating and dare I say it — shit. If you are just starting out with command line interfaces — your journey will be very rewarding and with considerably less friction.

Which Version Should I Use?

Naturally, you will want the latest version, but remember — many features are not cross compatible and this will likely manifest in your programs breaking. However, if you design your programs to be fault-tolerant, which I will teach you over this series of articles, it is likely that the version number you choose should not make too much of a difference.

Grab the latest version for your operating system over at github. For now, accept the defaults when you install PowerShell and don’t worry about anything else.

Enough Already — Show Me the Power in PowerShell

So Jane, Jeff, John or whoever comes over to you and says their program is acting weird. Usually it will be some Microsoft application but not always.

You head over to investigate or you remote onto their machine with your fancy pants remote desktop software.

Let’s now jump to the point where you have tried everything and this application is just being an absolute bastard and your only option left is to kill it off.

Your first instinct is to open task-manager, find the problematic application, right click the application and select end-task. This will work in most scenarios and is quicker and better practice than firing up PowerShell and typing some cmdlets. See Figure below. But what if this doesn’t kill off the application?

Figure — Killing off an application with task-manager

What if Task-Manager Doesn’t Kill off the Application?

You could do a number of things — logoff, restart etc but this is an opportune moment to flex your PowerShell muscles.

Fire up PowerShell. In Windows, the quickest way to do this is to hit the Windows key, search for PowerShell and hit enter.

CRITICAL ADVICE. Don’t be an operating system snob. I’m writing this document on a MacBook Pro that connects to a Windows 11 Pro Machine that runs Windows Server 2022 and Kali Linux. I suggest that you setup a multi-operating system environment. If you limit yourself to one operating system, your usefulness to solve problems is reduced and you’ll likely be perceived as less professional.

Our First Mini Program

Problem: You want to find the two most memory consuming applications and kill them off. This is relatively easy to do in task-manager but imagine finding the first 20 applications and individually clicking each application and selecting end-task. Not fun! Let’s use PowerShell’s automation prowess to help us out.

Solution: PowerShell. Often, real world machines run out of memory and you will have to free up some memory for the machine to become usable. See Figure below.

Figure — Finding memory hogs and killing them off

Discussion:

In one line of code, you can automate x amount of tasks. Imagine if x = 100. It would be a bad day at the office if you had to manually search for and click a hundred different things.

PowerShell’s true power is its ability to process objects. Here, we get all the processes on our system using the cmdlet get-process. We then send all those processes to another cmdlet called sort-object that sorts those processes by memory consumed and in descending order. sort-object then passes those sorted processes to the cmdlet select-object which selects the first two processes. Finally, select-object passes those two memory hogging processes to the stop-process cmdlet which knows how to kill them off!

What is truly remarkable about his whole process — no pun — is the fact that we use cmdlets or commands that make intuitive sense and read almost like a sentence. You could say it like this.

Get all processes and sort them in descending order of memory used. Then select the first two processes and kill them off.

PowerShell takes the cryptic command lines of the past and rewrites the usability rulebook to be usable NOT confusing.

Stay with me for part 3 where we will look at PowerShell’s exceptional usability features.

--

--