This blog series will teach you the basics of PowerShell. You’ll learn common concepts such as variables, loops, functions and scripts in this series, along with PowerShell unique concepts such as cmdlets and the pipeline.
- PowerShell: The basics – Part 1 – Getting Started
- PowerShell: The Basics – Part 2 – Cmdlets and Modules
- PowerShell: The basics – Part 3 – Data Types, Variables, Operators, and Functions
- PowerShell: The basics – Part 4 – Strings, Arrays, Hashtables, Conditional Statements, and Loops
- PowerShell: The basics – Part 5 – Filtering and Formatting
- PowerShell: The basics – Part 6 – Remoting
In this post we will take a brief look at what PowerShell is, and where you can run it.
PowerShell – What is it all about?
PowerShell is a scripting language that tell the operatingsystem or applications to do stuff. It’s powerful in the way that we can automate common tasks, create scripts or tools to aid our in our work, or solve complex challenges with just a few lines of code. PowerShell has quickly become the must have tool in every sysadmins toolbelt, and it you will help you become much more effecient and capable. There’s almost no limit to what you can accomplish in PowerShell!
PowerShell is included in Windows operating systems, so it’s always available to you when your working in a Windows environment. To work with PowerShell you need to open a program called Windows PowerShell, which is a command line interface (a console where you can write and run commands).
You accomplish this by clicking on the start menu and type PowerShell to search. We’ll look at the Windows PowerShell ISE later.
Note: You can safely ignore the (x86) versions, as they’re only included for backwards compatibility, but you’ll virtually never have to use them.
At this interface you can write and run PowerShell commands. To run a command you have written, you hit Enter. This interface is best suited for running short ad-hoc commands that fit on one line. PowerShell has been upgraded over the years, and depending on the version your running you may not have all functionality available. Check your version by issuing the following command:
The newest version included with Windows is Windows PowerShell 5.1, although a newer open-source version can be setup (version 6 and 7). You’ll most likely find that version 5.1 includes everything you’ll need. If you run an earlier version, you can get version 5.1 by downloading and installing Windows Management Framework 5.0.
After running some commands you may find the console a bit cluttered, and wish to clear everything off the screen. You can do this by issung the following command:
Everything you run will be saved in your history, which you can access by arrow up/down keys. This is very neat when you need to run the same command multiple times, and don’t want to type the entire thing all over again..
PowerShell is a case-insensitive language, so you can mix upper and lowercase in your commands as much as you please, but best practice suggest to use correct casing. To easily accomplish this, along with not havin to remember the syntax of every command, you can utilize the built-in intellisense tab completion. Just write the start of a command and hit tab to auto-complete the command. If what you’ve entered matches multiple commands you can continue to hit tab until your desired command shows up. This also works for variables and function parameters.
Integrated Scripting Environment (ISE)
You might have noticed the program called Windows PowerShell ISE earlier in this post. This is a program better suited for writing and running scripts in PowerShell. Scripts are PowerShell logic stored in files, to be run at a later time. You write scripts in the scripting pane, and hit F5 to run the script. Regular PowerShell script files have the extension .ps1. You can also place the cursor on a specific line, or highlight a portion of your code and hit F8 to only run that part of the script. Hit CTRL + S to save it to a file. Note that the results of the code you run show up in the console pane. You can also write and run commands directly in the console pane. Try yourself with the following code:
# This is a comment $a = 2 $b = 2 Write-Host "$a + $b = $($a + $b)"
Note: The Windows PowerShell ISE is no longer actively maintained by Microsoft, and you should eventually replace it with Visual Studio Code (VSCode), which is it’s successor. But VSCode has a much steeper learning curve, so we’ll stick with ISE for now.
When you write code in ISE you’ll notice a nice addition to the tab completion, where it visually shows you a drop down list of all commands that matches what you’re typing. You can tab or click on an item to auto complete, or you can hover over the item to view the commands syntax.
In the next post we’ll take a look at the PowerShell language and syntax. If you want more detailed reading on the topics of this post I suggest the following resources: