So you’re curious. I getcha. There’s probably a million “Getting started with PowerShell” tutorials out there on the internet; and now there’s a million and one. I’m hopeful the ideas in here will help even the non-tech-minded person come around to understanding what PowerShell is and how it is used. I’ve written an even more casual introduction already, so in this post, we’ll actually use PowerShell.

PowerShell Shortcut Icon

If you’re on a Windows PC… search for PowerShell in the taskbar and open it up. It comes with the operating system, you don’t need to install anything.

Depending on what version of PowerShell/Windows OS you are running, the opened window may have either a blue or black background. In any-case, here’s a quick list of different names you’ll see people on the internet calling this window:

  • terminal window
  • console
  • command line
  • PowerShell
  • shell
  • the interpreter
  • session window

I’m sure there are people seething in their chairs at that list because they know the difference between all of those terms. As a beginner- know that people will habitually refer to the PowerShell command line terminal shell interpreter console session window by whatever name is most familiar to them for whatever reason. In my opinion, ‘terminal window’ or ‘PowerShell window’ is the ‘most correct’, but just be aware of other lingo you may come across elsewhere when looking up tutorials etc.

Whatever we call this window, what we type in here will be ‘interpreted’ line by line, as PowerShell code. Type this into the PowerShell window and press enter. It will output Hello World back at you:

Write-Host "Hello World"

Simple. Did you try and copy/paste the code? Note that keyboard shortcuts and using the mouse don’t work as expected in a PowerShell window. Just really quickly, to avoid frustration while you’re starting:

  • You can right-click to paste text.
  • Pressing Ctrl+C with nothing highlighted will cancel the current line you are typing- or ‘interrupt’ and stop whatever action PowerShell is currently undertaking. It’s called a ‘keyboard escape’.
  • Pressing Tab will ‘auto-complete’ whatever you are writing. You can keep pressing tab to cycle through auto-complete options. Try typing wr and pressing tab a few times. You’ll see PowerShell cycling through different Write- commands (Write-Host will come up eventually).
  • This is not a complete list of nifty keyboard shortcuts and interface tricks that can be done with PowerShell. There could be a whole blog post dedicated just to them.

Is this ‘computer programming’?

Just for a hypothetical: We could stop here and say that we have written a “PowerShell program”. It is one line of code long, and its purpose is to print the text ‘Hello World’ to the terminal window.
If we wrote the ‘program’ down into any text file (by using the Notepad app or similar), and saved it with a .ps1 file extension (eg. named the file “Hello_World.ps1”), then we would have our first PowerShell script. Any-time we wanted to see the words “Hello World” printed to the terminal, we wouldn’t need to manually type out the line of code. Instead, we could just tell PowerShell to ‘read’ the “Hello_World.ps1” script, and follow the coded instructions therein.
Programmers are people who write complicated scripts with many (hundreds if not thousands) of lines of code, that all interact with each-other to form into one large synergistic “program”. Typing commands directly into the PowerShell interpreter one at a time, is-and-isn’t technically ‘programming’. However it is a completely valid use of PowerShell, and a fundamental part of programming. Being that it is a command-line shell is something to be taken advantage of (and part of the name). Instead of writing your script, compiling it, executing it, then receiving your output (as you would with other languages); PowerShell opens the door for you to ‘interface’ with your computer directly. You can input commands, read their output immediately, and then use that feedback to determine what your next command will be. This is why it is chiefly used by IT professionals to debug, monitor and audit computers running Windows. You can develop programs and be a “programmer” who writes PowerShell code, and I’ll cover that in another blog post. While learning the fundamentals- let’s stick to writing code directly into the interpreter.

Now, let’s add some ‘parameters’ to our Write-Host command:

Write-Host "This text is colourful!" -ForegroundColor Green

This command will write the text in green. Let’s break down the syntax here:

Cmdlets

Write-Host is a ‘command’. PowerShell commands are called ‘cmdlets’, (pronounced ‘commandlets’- as in ‘piglets’ or ‘booklets’), and you’ll notice they follow a Verb-Noun schema. There are innumerable PowerShell cmdlets out there to experiment with. Some with ‘general’ purposes (like Write-Host), while others fit incredibly niche use cases and you may go your entire career without ever using them/knowing they exist. Being great at PowerShell does not mean being a human dictionary of cmdlets, but having an extensive knowledge of the various tools in your toolbox here can go a long way. Just so you’re familiar with the Verb-Noun schema and can get used to what cmdlets ‘look like’, here’s a few other quick examples:

  • Get-Command
    • This lists all the PowerShell cmdlets that are installed on the computer.
  • Get-Help
    • This cmdlet will show you the documentation for any other cmdlet.
  • Get-Process
    • This will output information about any processes running on the computer.
  • Stop-Process
    • This will stop a process that is running on the computer.
  • Get-Item
    • This will list the contents of a folder on your computer.

Parameters/Arguments

"This text is colourful!" or "Hello World" from the examples above, is the argument that has been passed to Write-Host. We write ‘arguments’ after our cmdlets that give PowerShell a little extra direction towards what we want it to do. In this case, Write-Host knows to write text to a new line in the terminal window. It doesn’t know what text to write, so we provide the text as an ‘argument’. For another example of argument use; the aforementioned Get-Help cmdlet takes other cmdlets as arguments, for example:

Get-Help Write-Host

Moving on, -ForegroundColor is a parameter for the Write-Host cmdlet.

Parameters are marked with a - hyphen at the beginning, and just like arguments, they change the way the cmdlet behaves. As you’ve seen, the-ForegroundColor parameter changes the colour of the text that Write-Host outputs.

All cmdlets have optional arguments and parameters that can change the cmdlet’s ‘default’ behaviour (such as -ForegroundColor changing the default colour of the Write-Host output).

Some cmdlets have arguments or parameters that are required for the cmdlet to run. For example, if you input Stop-Process without any arguments or parameters; PowerShell will not know which process you wish to stop. Since it does not know what to do without any arguments, they are said to be ‘required’.

Some parameters can also take their own arguments. Green is the argument that we gave to the -ForegroundColor parameter. You could try changing Green to Yellow or Cyan or Red and you’ll see how passing different arguments to-ForegroundColourchanges the cmdlet’s behaviour. If you ran Get-Help Write-Host from the above example, you’d see this list (below) of parameters and arguments that Write-Host accepts:

NAME
    Write-Host

SYNTAX
    Write-Host [[-Object] <Object>] [-NoNewline] [-Separator <Object>] [-ForegroundColor {Black | DarkBlue | DarkGreen
    | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow |
    White}] [-BackgroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray |
    DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}]  [<CommonParameters>]

As you can see under Get-Help‘s “SYNTAX” section, Write-Host also accepts the parameter -BackgroundColor (among others like -NoNewline and -Separator). The arguments that are passable to each parameter are also listed beside it.

Let’s try another line of code with two parameters and some different arguments (different colours) taken from this list:

Write-Host "This is really obnoxiously colourful text!" -BackgroundColor Magenta -ForegroundColor Yellow

Broadly speaking, “writing PowerShell code” is,
“Using cmdlets and their different parameters effectively.” Now you’ve seen the Write-Host cmdlet in action, and even passed it a variety of arguments and parameters.
I know this seems like a totally useless thing to learn (and, granted- you probably don’t need to know “how to write yellow text on a magenta background in PowerShell”)- but hear me out!
As you get deeper into the shell and start using it for actual ‘purposes’, your command-lines and their arguments/parameters etc, are going to become more intricate. As such, it’s really important that you get a solid fundamental grasp of passing parameters and arguments to cmdlets. In my view, syntax-wise, it’s one of the most ‘different’ things about PowerShell compared to other programming languages like Python, JavaScript or C. Passing parameters to cmdlets using spaces and hyphens etc may come naturally to you if you are a total beginner. But if you have any experience with other (non-shell) languages; it may seem a bit strange. That’s why I say we go slow for this part.

My view on the ‘meta’ of PowerShell

So Write-Host ”echoes” text back at you. Well, its not of the upmost importance that you know this, but the word echo is an ‘alias’ for (a very similar command) Write-Output:

echo "Hello World"

Type that into the window and press enter. Same thing, right? This is because echo is a command from Unix-based operating systems, and the folks that developed PowerShell knew that many IT professionals would have Unix commands or even DOS commands already drilled into their minds from years of past experience. So they pre-built ‘aliases’ into PowerShell for a-lot of the most commonly used IT-commands of yore. Some other quick examples: Set-Location is a PowerShell command with the Unix-friendly alias cd; New-Item is PowerShell, inputting Unix’s mkdir works the same too. Why should you be aware of this as a beginner? Two reasons:

First, if you happen to be familiar with any other command line languages, odds are the commands you already know may also work in PowerShell natively.

Secondly, but perhaps more importantly- I think it demonstrates something that is good to understand about the ‘intention’ behind PowerShell’s design. This is meant to be a flexible, user-friendly tool that you can use ‘on-the-fly’. The creators of this language (Microsoft) didn’t want to force everyone using Windows OS to need to learn a whole new command-line language; instead, they aimed to ‘modernise’ the current command-prompt interfaces that IT professionals were already well-familiar with.

Writing code in other languages usually means getting your syntax exactly right, 100% of the time. One semi-colon out of place here, or one wrongly capitalised letter there- could crash your entire program. PowerShell on the other hand, is generally more forgiving. You can input WRITE-HOST write-host or even WrItE-hOsT and PowerShell will still interpret the command. Parameters work the same way. If this is your first foray into programming, then be warned: Luxuries like these aren’t afforded to you with most other languages. Being that PowerShell is so forgiving, it’s easy to fall into bad habits. This leads many programmers to say it should be ‘avoided’ as a beginner’s first-language; because it doesn’t instil the regimented approach to formatting and syntax that prepares you for other languages like Python (which uses line indentation) or JavaScript (which requires semi-colons to terminate lines of code), both are case-sensitive, etc. I say these naysayers are elitist snobs. The key to being a good programmer is (in my belief) a creative understanding of the underlying “logic” behind a program, script, or function. Nobody is considered a “good programmer” because they nail their line indentations in Python every time- these are merely ‘habits’ you need to pick up to become an efficient programmer.

More Power

We haven’t even scratched the surface of what PowerShell is capable of (we didn’t even define a variable!); but we have taken some foundational steps, and philosophised on why PowerShell is the way it is. You can find further steps to take on your PowerShell journey (ugh, I hate when they call things like this a ‘journey’) at this here hyperlink.

Remember:

Verb-Noun -Parameter argument

You can’t go wrong.