PowerShell: Automation for the IT World

Posted by:

|

On:

|

Those in IT Infrastructure and Operations love using PowerShell. If you’re not familiar with it, you’ve probably met someone that uses it. Maybe the admin finally looks at the computer, opens a prompt, and types something which finally fixes that window popping up. Or a coworker takes a spreadsheet, runs a program and makes a change to hundreds of users in seconds rather than clicking through menus for forty-five minutes. It’s likely that many places have programs in place that create user accounts whenever HR adds an employee to the system.

Simply put, it is task automation that can be used in the terminal or as scripts.

PowerShell as a Command-line Shell

In IT-land, PowerShell is the go-to for quickly completing out tasks in a shell console. PowerShell adopts the commands of popular shells as aliases, as well as in addition to being able to parse .NET objects. This means you can type commands similarly to the way you would on other platforms and achieve the same result. The .NET objects make PowerShell especially helpful for working on Windows machines as much software is built using the .NET framework. For instance, the classes created in a C# program can be modified with PowerShell to change settings or pulled to use information elsewhere.

PowerShell is flexible and often easy to use compared to other languages despite your background in scripting or command-line programming. These commands will allow you to see folder contents of the respective systems:

Windows:

dir

Linux:

ls

PowerShell:

PowerShell
Get-ChildItem

When you run any of these commands in PowerShell, they do the same thing as they are all added as aliases. So, dir, ls, or gci will automatically be converted to Get-ChildItem. Notice that PowerShell’s commands are more verbose. We might be able to assume that dir means “directory” and ls means “list”, but Get-ChildItem plainly does what it says in English.

All commands come in Verb-Noun format, so commands will start to become natural. You can type Get-Command or Get-Verb to see more examples.

PowerShell as a Scripting Language

The IT crowd commonly uses PowerShell to automate system management, like Active Directory modifications or workstation configuration. Just about anything you can do in a menu, you can do faster with PowerShell.

While it’s useful to bang out a one-liner in the terminal, it’s also possible to create executable programs in PowerShell. In fact, it’s capable of creating functions, classes, and modules for use in scripts. It also supports formatting data and can parse CSV, JSON, and XML. This allows for complex tasks on many types of files and has massive scalability. You can install modules for all sorts of systems, and manage workstations, Windows servers (including Exchange), SQL, VMWare, and the major cloud providers.

How do I learn this great power?

It’s all practice and repetition. You don’t need a computer science degree, math skills, a card from Mensa or even IT experience to understand. It is the goal of this site to atomize tasks and provide ways to complete them, broken down to the simplest level.

This website will refer to the open-source, cross-platform PowerShell unless otherwise stated. Be aware the Windows-only version, PowerShell Core has a small number of commands that are not backwards compatible. For easier editing, Microsoft’s VSCode is a fast, lightweight code editor, and has a great PowerShell extension developed by Microsoft.

Other than that, playing around with commands and learning how to do perform your everyday tasks will take you far.