PowerShell: The Basics
Tags: powershell, tech, technology, mirosoft
Published: 2022-02-13 00:25:03


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644687849701/KDfWlS0mK.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.
 <hr /> 
### 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.
 <hr /> 
### 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644687590137/MiGgWTxXkH.png)

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644687814623/M01UJ1l3K.png)

The environment offers a x86 / 32 bit alternative for every application/tool as well:
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644687830635/AV755uNov.png)

<hr /> 
### 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](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_objects?view=powershell-7.2#long-description)

 <hr /> 

### 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](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7.2#long-description)

 <hr />

### 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644690476041/KbsOpWfyM.png)

or in a script 
![Variable in script](https://cdn.hashnode.com/res/hashnode/image/upload/v1644690498143/9LU1iqkEi.png)

<hr/>

### Follow the rabbit 🐰

[Windows Developer: trailer - The new Windows Terminal](https://www.youtube.com/watch?v=8gw0rXPMMPE)

[How-to-Geek: The New Windows Terminal](https://www.howtogeek.com/673729/heres-why-the-new-windows-10-terminal-is-amazing/)

[Microsoft: PowerShell Documentation](https://docs.microsoft.com/en-us/powershell/?view=powershell-7.2)