PowerShell: The Basics

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 e…


This content originally appeared on DEV Community and was authored by Montè

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


This content originally appeared on DEV Community and was authored by Montè


Print Share Comment Cite Upload Translate Updates
APA

Montè | Sciencx (2022-02-13T00:25:03+00:00) PowerShell: The Basics. Retrieved from https://www.scien.cx/2022/02/13/powershell-the-basics/

MLA
" » PowerShell: The Basics." Montè | Sciencx - Sunday February 13, 2022, https://www.scien.cx/2022/02/13/powershell-the-basics/
HARVARD
Montè | Sciencx Sunday February 13, 2022 » PowerShell: The Basics., viewed ,<https://www.scien.cx/2022/02/13/powershell-the-basics/>
VANCOUVER
Montè | Sciencx - » PowerShell: The Basics. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/13/powershell-the-basics/
CHICAGO
" » PowerShell: The Basics." Montè | Sciencx - Accessed . https://www.scien.cx/2022/02/13/powershell-the-basics/
IEEE
" » PowerShell: The Basics." Montè | Sciencx [Online]. Available: https://www.scien.cx/2022/02/13/powershell-the-basics/. [Accessed: ]
rf:citation
» PowerShell: The Basics | Montè | Sciencx | https://www.scien.cx/2022/02/13/powershell-the-basics/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.