PowerShell: The Basics

c0der4t

Montè

Posted on February 13, 2022

PowerShell: The Basics

image.png
Ah yes, PowerShell, the tool some techies avoid and users fear.

PowerShell has a vast command inventory and learning it all in one post…impossible. But, for so many techs in the field PowerShell is an alien concept with which they have little to no experience. So today, we’ll have a look at the basics you need to know to get started with PowerShell.


Why should I learn PowerShell?

PowerShell is at the forefront of Windows automation and being able to utilize it will make you a force to be reckoned with. Additionally, it allows you to tap into previously unusable resources and functions to solve technical issues and implement changes.


The PowerShell environment 🏴

The PowerShell environment is sectioned into multiple versions and tools for various scenarios.

We have the PowerShell CLI or command line, used to run commands on demand, like CMD:
Menu Entry for PowerShell CLI

We also have the ISE or ‘Integrated Scripting Environment’, which aids in writing PowerShell scripts (.ps1 scripts) and allows for easy testing and debugging:
image.png

The environment offers a x86 / 32 bit alternative for every application/tool as well:
image.png


Objects 📜

In PowerShell, data is contained and transported in 'Objects'.
Objects can contain a lot of data pieces/nuggets. This 'data collection' can be filtered or queried in line to access specific pieces of data from the collection to either process, display or pass as input to the next command.

Objects can be assigned to variables, allowing re-use or more complex processing.

Read more on objects here 👉 Microsoft Docs: about_Objects


The Pipeline operator ( | )

PowerShell allows you to chain commands and actions, passing the output from one into the input of the next. This allows you to write automation scripts, as commands can now "talk" to each other and execute commands reactively based on the output of a previous command:

Get-Process notepad | Stop-Process

Here we get a process object with notepad in the name. We 'pipe' the out (the process object) into the next command which uses the information in the object to terminate it.

Note that you can chain multiple commands, one after the other with each new command accepting the output of the preceding command as it's input.

Read more on pipelines here 👉 Microsoft Docs: about_Pipelines


Variables 📦

Variables are used to store information to be referenced and used by the system in subsequent commands. Variables (var(s)) mutable, meaning their values can be changed as commands execute and outputs are generated.

In PowerShell, variables are marked by a dollar sign ($) prefix.

The pipeline variable ($_)

The pipeline variable's value is equal to the output of the preceding command and can be used in the subsequent command to allow filtering and querying of objects.

The pipeline variable can only be used with a command spread of 1, this means that referencing $_ in command 3 will be equal to the output of command 2, not 1.

The pipeline variable is always denoted by $_ and object properties can be referenced with the format $_.propertyname :

Get-ChildItem C: | where { $_.PsIsContainer -eq $false } | Format-List

The traditional variable

You can declare a traditional variable in PowerShell by prefixing a text string with the dollar sign ($)

in a live session
Variable in live session

or in a script
Variable in script


Follow the rabbit 🐰

Windows Developer: trailer - The new Windows Terminal

How-to-Geek: The New Windows Terminal

Microsoft: PowerShell Documentation

💖 💪 🙅 🚩
c0der4t
Montè

Posted on February 13, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

PowerShell: The Basics
powershell PowerShell: The Basics

February 13, 2022