What is PowerShell
A lot of people don’t know what PowerShell is, they have heard about it but don’t know what it is exactly. It’s similar with other shells, so the best introduction to PowerShell we can do is to define it. Let’s do it.
„PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language. Unlike most shells, which accept and return text, PowerShell is built on top of the .NET Common Language Runtime (CLR), and accepts and returns .NET objects.“
Basically, PowerShell is a windows shell scripting language to write commands called cmdlets and combine them into complex scripts. It gives you the complete access to a computer, assuming you’re the admin of that computer. In this introduction to PowerShell you’ll learn basic concepts and some examples.
NOTE: If you can, you should always open PowerShell as an administrator because PowerShell can block some commands, specially destructive commands, for users that are not admins on that computer. The problem is that you won’t get an error like ” only an administrator can run this command” or something like that, the error will not tell you that you should be an admin. So, it’s best to prevent that in the start, if you can.
The beauty of PowerShell
The beauty of PowerShell is that it doesn’t return text like most other shells. PowerShell returns .NET objects which you can use and manage like regular objects. Why is that so awesome? Well, if you get, let’s say, a list of services listed in Powershell, you can select which row and/or line you want and manage it separately.
In text based shells like Bash, you can do that to some extent with some filter commands, but you’ll often end up with a messy text that needs to be cleaned. On the other hand, in PowerShell, you can just convert your output to JSON and you have a clean and ready JSON to work with. We’ll show all this later in this article.
PowerShell supports command aliasing which means you can use different names for commands like clear-host command that clears the screen. You can also use clear or cls. The famous commands like ls is also supported, native PowerShell command for that is Get-ChildItem. It’s worth mentioning that aliases are great for single commands and smaller scripts, but for big scripts it’s better to use native commands for better performance and readability.
Cool aliases for Linux users
We’re also gonna have a little section for Linux users in this introduction to PowerShell. We’re going to mention some of the most important and well known Bash commands that are also aliases in PowerShell and can be used there as well.
- ls – Get-ChildItem
- cat – Get-Content
- ps – Get-Process
- clear – Clear-Host
- curl – Invoke-WebRequest
- kill – Stop-Process
- man – help
- pwd – Get-Location
- wget – Invoke-WebRequest
- tee – Tee-Object
How to use PowerShell
Let’s start with a simple example since this is an introduction to PowerShell. We’re gonna give some more complex examples in the next article.
This command lists out all the commands you can use. We won’t show the result here because it’s quite long, but you can see all available commands with this command. With the next command, you can get help for a certain command. Like this:
With this, you can find all commands and see how they work. Pretty neat, right?
Get-command and Get-help commands are very helpful and you can discover all the commands and their syntax with them. However, Microsoft has a great documentation on their official docs. They have a dedicated page for every PowerShell command, why is that you might ask? Well, you can access that page through PowerShell.
It’ll automatically open the Microsoft docs page of that command in your browser.
Pressing tab in PowerShell enables you to go through all the options that you might enter which is priceless is you’re writing something new. You don’t have to read the documentation every time you need something or try to work around something not knowing that there’s an easier way. See the gif bellow to see how tab works. It’s like an autocomplete in Visual Studio.
Pipeline is a very powerful tool that enables your to use the results from your previous command for another command. Usually it’s used to store something, to parse something, to convert something, etc.
Testing and script development
In this introduction, we’re also gonna tell you about a few ways to test and work with PowerShell.
Whatif is a PowerShell parameter that let’s you see what a certain command execution would do, without actually doing it. You can use it, for example, on commands you’ve never tried before to see what they do and how they behave.
Not all commands support -Whatif parameter and there’s a simple way to check if one does.
As you can see, running Get-Help command with particular command as a parameter shows you if you can run -Whatif parameter with that command. It’s shown under Syntax section.
Now let’s run a command with -Whatif parameter to see how it works.
You can see that after running the Stop-service command with -Whatif parameter didn’t stop the service, it only told you what it would do if you ran the command.
PowerShell ISE stands for Integrated Scripting Environment and it’s and environment for writing and testing PowerShell scripts. Great thing about ISE is a built in help and the ability to build scripts. Every time you execute a command in any shell, it just writes some output and if you want to build your command, you have to bring it or write it back and add some more code to it. The ISE offers your the ability to change and write your scripts until you’re happy with the result. It also list’s you all the commands with their explanation.
Another great feature of PowerShell ISE is the ability to build commands as seen on the right bottom part of the picture above. You pick your command and enter your parameters and you can automatically run the command, insert it to see how it looks or copy it to your editor above for your script. Then you can run it after adding other commands.
Our particular command was Out-File and that command writes the output to a file on your PC.
Visual Studio Code
There’s a PowerShell extension for VS Code that you can install.
So when you’re working on a project and you need to run a quick command, you can do that from your VS Code project directly.
PowerShell Core is cross platform version of PowerShell that enables you to use PowerShell on MacOs, various distributions of Linux and of course, Windows. But you have regular PowerShell on Windows so there’s really no point in getting PowerShell Core.
PowerShell in automation
If you haven’t already, you should read our articles on what automation is to better understand this introduction to PowerShell in automation. We’re gonna go more in depth in our next article.
So, PowerShell in automation, how does it work. On a Windows system, anything that requires a shell task is done in PowerShell, and you can do a lot. Gather system and service info and status, stop, start and restart services, machines, connect to a remote server, etc. Let’s take a simple example.
Let’s say that you want to automate a simple service monitoring to see if any service suddenly stopped for some reason. You can check a service status really quickly in PowerShell, so just do that for a number of services at once. It’s done using Foreach loop.
Of course you don’t need all 3 ways to output the same result. You’d only use one, the first one to directly see what’s going on or the last one if you want to pass the data on to parse it and make a nice table out of it. It’s JSON after all. You can have a simple script like that on your machine and automate it to send you a JSON every 1 hour and than parse it into html table and send it to your mail. That last part you can do through Windows Task Scheduler.
NOTE: When you convert your output to JSON, you can see that the status changed form description to number. Powershell does that sometimes and you have to account for that.
Another neat idea for a script would be to check every half and hour if services are running, if a certain service is stopped, then restart it.