PowerShell 101 – The absolute basics

There are so many PowerShell 101’s out there but I still felt I needed to write one. So many IT people I meet still don’t know the basics of PowerShell. So if you want to get started with PowerShell please keep reading.

What tools are out there?

You want to start scripting, first you need to get is some tooling to write. My personal favorite is the, in Windows integrated, PowerShell ISE (Integrated Scripting Environment). I love the combination of being able to write code, have tab completion, select and run specific code and the command-line where you can run code like you’ve a normal PowerShell window open. Additionally you can use notepad++ for a simple text editor with highlighting or Visual Studio Code which is a more professional tool and a slimmed down version of Visual Studio Standard.

PowerShell 101
The PowerShell ISE

Those are the editing tools. What do we have inside PowerShell to get started? The combo “Get-Command” and “Get-Help” will help you get any basic info. Because most cmdlets (that’s what commands are called in PowerShell) are pretty logically named you can easily use “Get-Command -name “*service*” to retrieve any cmdlets that have “service” in the name (see below for example).

Subsequently you can use “Get-Help” to get additional information about a cmdlet you just found. Most cmdlets have extensive help information, parameters and examples on how to use the cmdlet. Don’t forget to add the “-full” parameter if you want all help information on a cmdlet. Noteworthy is that you can use “Update-Help” to update your local help content directly from Microsoft.

The last and probably most useful tool (more of a skill really) is being able to “cut up” your idea for a script into pieces. You are at point A and want to get to Z. If you can visualize/specify points B to Y you can easily google the small snippets of code you need. There is no need to do everything yourself while you can easily find pieces of code you need and integrate them into your script.

This I think is the most important lesson of this 101. Google is your friend. Especially if you’re in a rush and want to get the job done quickly. Google it! Just search for whatever you need to get done and most of the time you’ll find someone who had the same task/issue. Whenever writing more complex scripts you can google for specific functionality.

It’s object based

That sounds cryptic but it basically means that info you get back from cmdlets is not just text only but it consists of properties (information which could be text) and methods (things you can execute). Take this example with Get-Service (which gets you a list of services).

First line we get the windows service with the name “Spooler” and we save the information into a variable called “SpoolerService”. The “$” marks a variable name. Variables are useful whenever you want to save something for later use.

Second line we actually start using the variable. First we retrieve the “StartType” property. Which comes back with a piece of text “Automatic”. So now we know how to access properties.

Third line we use a method. I invoked the “WaitForStatus” method which obviously waits for a service to enter a certain status (I wrote a post about this earlier). Between the parenthesis I enter the status I want to wait for, in this case “Running”. Since the service is already running it directly continues.

Variables

As I said earlier you can save stuff for later use in variables. There are a lot of different types of variables out there and I’m not going to discuss all of them. I’ll discuss a few of the basic ones to get you started.

  • [String] is the most basic of variables. It only holds some text.
  • [Integer] same as string but can only hold numbers. Nice when you want to do some math.
  • [Boolean] is a switch value. It’s either $true or $false.
  • [Array] a list of multiple values. The separate values can be of other type.

That’s the first PowerShell 101 for now. In the future I’ll add more posts with more advanced info on PowerShell.

 

PowerShell 1XX post series

  1. PowerShell 101 – The absolute basics (current)
  2. PowerShell 102 – Practice scripts
  3. PowerShell 103 – Pipelines
  4. PowerShell 104 – Operators

3 thoughts on “PowerShell 101 – The absolute basics

Leave a Reply