Productive in PowerShell - Getting Started With Basics

22 Sep, 2019

containers

Photo by Gia Oris

It’s the first post in a series, where we use PowerShell to get things done faster & look cool 😎 while doing it. This post covers the basic concepts & terminology of PowerShell to get you up & running. You don’t have to be a terminal geek to eat this fruit of productivity (that didn’t come our right)

What the Hell Is PowersHELL? (pun intended)

PowerShell is task based command line shell & scripting language built for automation & administration. It structures information in form of objects unlike other CLI that deal with stream of text. It’s integrated & shipped as part of every Windows version ever since Windows 7. It now comes in two variants, one that’s shipped with Windows & one that’s available for download

PowerShell Variants

Windows PowerShell

  • Versions between 1.0 and 5.x
  • It’s proprietary software & closed source
  • Built on .NET Framework
  • Runs on Windows only
  • Only receives bug fixes, kept on life support for backwards compatibility
  • Comes with Windows (for now)

PowerShell Core

  • Versions from 6 & above
  • It’s Open Source
  • Built of .NET Core
  • Runs on Windows, Linux & Mac
  • Ongoing development adding new features
  • Needs explicit download & installation

Concepts

PowerShell has three core concepts & they apply irrespective of the version

Cmdlet (Command-let)

  • It’s a lightweight class written in .NET to do a specific task when invoked by PowerShell runtime
  • It’s named in two parts: a verb & a noun separated with a hyphen (Ex: Get-Alias)
  • Almost all cmdlets follow the single responsibility principle
  • Around 986 cmdlets come packaged with PowerShell on Windows 10

Pipeline

  • Pipe | operator forms the bridge between two cmdlets by creating a pipeline
  • The output of each command gets passed as an input to the next one in the pipeline
  • Ability to create complex pipelines justifies the single responsibility nature of cmdlets

Function

  • It’s a PowerShell script & a simpler alternative to creating cmdlets in .NET
  • It behaves almost like a cmdlet with minor differences
  • It can be turned into a cmdlet by decorating with special attributes

Terminology

Alias

Alias is an alternate short name assigned to a cmdlet, function, script, file or executable. PowerShell comes with a predefined set of aliases & also supports creating custom ones

Scripts

Long PowerShell scripts can be saved into a file with an extension .ps1 & executed by the runtime

Module

A module is a package containing cmdlets, functions & aliases. They can be accessed in any PowerShell session by loading the module

Commands

PowerShell commands include cmdlets, functions, aliases & scripts

Get Started

To get started, open PowerShell & use these cmdlets to explore

  • Get-Help to display help for any command
  • Get-Command to list all the available commands
  • Get-Alias to list all command aliases

Keep watching this space for next post