Categories
Development Powershell Tutorials

Powershell – Introduction

Powershell logo

“One tool to rule them all”
“Those who don’t automate are doomed to repeat themselves”

Powershell, the command line framework and scripting language from Microsoft.
In the beginning this was a Windows only feature (Windows Powershell) but has since Powershell Core and
Powershell 7 gone to open-source and is available on other platforms.
Since I am a Windows guy we’ll be focusing on Powershell from a Windows perspective.

Windows 10 comes with Windows Powershell 5 preinstalled.
Simply type Powershell into your start menu’s search bar and you’ll see two applications:
Powershell and Powershell ISE (x86 and x64)

Powershell or Powershell.exe is where you write Powershell one line at a time, much like the
Command prompt (CMD) that many has seen before.
The commands used in Powershell is called ‘cmdlets’ and are named like Verb-Noun.

Powershell ISE is the other application which ships with Windows and it has been the primary editor for Powershell scripts.
Microsoft has now stopped developing the ISE and is instead promoting the use of Visual Studio Code (VSC) with a Powershell plugin.
Visual Studio Code is free to download and use. https://code.visualstudio.com/download
ISE is still very much usable but VSC is the future and if you do come from a programming background then VSC will most likely be preferable.

So what can we do with Powershell? Well everything basically!

But a good place to start is with the cmdlet Get-Process.
This will show the processes running on your computer.
See detailed information here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-process

Powershell is case insensitive, get-process/Get-Process/GET-PROCESS etc. all work the same.

Lets add a parameter, -Name and then lets grab all processes named Chrome:

Get-Process -Name Chrome
Using Get-* cmdlets is risk free since you only get information.

Consider this scenario: You’ve been using an application, in our case the built in calculator, and the program has stopped responding and you are now unable to close it.
You open up Powershell. You know about the Get-Process cmdlet and that you can use the Parameter -Name to only get processes named ‘Calculator’.
But now what?
Along comes the mighty Stop-Process !

This cmdlet, much like its more harmless sibling, also features the parameter -Name.
But there are two ways of doing this:

Stop-Process -Name Calculator
Get-Process -Name Calculator | Stop-Process

Both will have the same outcome, the process Calculator will close.
The second alternative uses the character | called ‘pipe’ to, you guessed it, pipe the information from Get-Process to Stop-Process.
This is something that is used in Powershell all the time, expecially outside of ISE where you only work one line at a time.

When you run the Get-Process cmdlet you are returned an object, this object holds information like ProcessName or ID of the process and much more.
When you pipe the Get- cmdlet to the Stop- cmdlet, your are then redirecting that object from being displayed on your screen to being used to fill in the parameters of the Stop- cmdlet.
This means that you can’t pipe whatever you want to everything but these two are in the same ‘family’ and have been built to work togheter.

The object being pushed through the pipeline is saved in the temporary variable called ‘$_’.
We will talk more about variables in other posts but think of variables like a tool chest with labeled drawers.

That’s it for this introduction to Powershell as I’d like to keep these bite sized for beginners.
A big part of learning powershell is to just test it yourself and exploring what happens (just be a bit careful with the Stop-Process or you might have to restart the ol’ computer 😉 ).

As always, continue to learn and evolve your skills, see you in the next one!

Do you want to know more? Here is a list of tutorials to check next

One reply on “Powershell – Introduction”

Leave a Reply

Your email address will not be published. Required fields are marked *