PowerShell
A Comprehensive Guide to Windows PowerShell
Table of Contents
A re you looking for an easy way to simplify and automate administration of all your Microsoft products? You cant go wrong with Microsoft PowerShell, one of the most powerful scripting languages ever devised. And dont let the term, scripting language scare you off either; PowerShell is remarkably easy to learn and has one of the most comprehensive support systems of any programming or scripting language.
PowerShell makes it easy to retrieve data about your system settings, manage your services, change objects and much more; learning it now will help you to reduce how much time you have to spend, or waste as the case may be, on admin functions and all without the need to purchase expensive tools or services from elsewhere. It comes built into Windows 10, so you dont even have to go through the hassle of downloading and setting it up its all there, just waiting for you to use.
In this guide, we will cover:
- What PowerShell is and what its uses are
- PowerShell cmdlets (commands)
- PowerShell scripts
- PowerShell strings and quotes
- PowerShell Automation and PowerShell Remoting
Rather than having one underlying theme running through the book, I have set it out so that you can pick and choose the chapter you want to read, depending on your needs at the time.
Welcome to the wonders of Windows PowerShell.
Chapter 1: Introduction to PowerShell
L ets start with the basics what is PowerShell and what can it do for you?
These days, there are multiple ways for you to interact with a computer system and manage it from the GUI (graphical user interface) to the CLI (command-line interface). Some people consider the latter as a huge backward step, back to the time of those old-fashioned green-screened terminals. However, today we still use it, together with web-based interfaces and API (application programming interface) calls.
But why have we gone back to the command line? To understand that, you need to understand what goes into large-scale computer system administration. Repetitive tasks take up a lot of admin time, and it is necessary to do these as quickly and efficiently as possible, especially with multiple systems requiring management. Add to that the need to make sure those tasks are done the same way every time to ensure the right results and you start to understand why the command-line interface method is so important. One of the most common command-line interfaces is Microsoft PowerShell.
What is it?
PowerShell is an automation and scripting platform developed by Microsoft. Built on the .NET framework, it combines an interactive command environment with a scripting language, and has been described by Ed Wilson, a leading resource on PowerShell, as follows:
Windows PowerShell is an interactive object-oriented command environment with scripting language features that utilizes small programs called cmdlets to simplify configuration, administration, and management of heterogeneous environments in both standalone and networked typologies by utilizing standards-based remoting protocols.
Thats quite the definition! Lets delve into it a bit deeper.
Explain Object-Oriented
In essence, an object-oriented programming language is a kind of logic, a way of understanding how a language or platform behaves. Everything in the language is an object. Python is perhaps the most famous of all object-oriented programming languages.
An object has at least one attribute and at least one method or function have a look at some real-world examples:
- A TV remote control attributes include color, shape, size, how many buttons it has, etc. Functions or methods include volume adjustment, changing channels and turning the TV on and off.
- A vehicle attributes include current speed, location, license plate, etc., while methods include moving, acceleration, slowing and parking.
- A dog attributes include breed, color, energy levels and mood, while methods include barking, sleeping, running, playing, etc.
Explain Cmdlets
Short for command-lets, a cmdlet is a PowerShell command and there are lots of them. Each cmdlet is responsible for a specific task or function, and well be covering them in detail in the next chapter. Behind a cmdlet, a lot is going on. When it is executed, the command works with multiple objects, methods, classes, API calls, and lots of other things, all to get its job done. The primary advantage of using PowerShell is that there is no need to understand all of this; the cmdlet takes care of everything.
To help you in using these cmdlets, PowerShell uses a naming pattern, verb-noun, to help you understand what each one is for. Some of the verbs you will encounter include Set, New, Get, Copy, and Add. Putting the verbs and nouns together gives you cmdlets like:
- Get-Process
- Get-Help
- Get-Member
What Are PowerShells Uses?
P owerShell has multiple uses and the only limit is your own creativity. The fact that its functions include scripting tools and an interactive language means that it makes system administration easy and provides IT professionals with a ton of flexibility.
When you use PowerShell as a CLI for direct system integration, you benefit from being able to connect to another system remotely. Using a remote PowerShell session, you can easily connect to a server that is in a different physical location and run commands as though you were on that server. There are lots of system tasks that can be remotely done, potentially saving you tons of time. Not only that, administrators can run the same command simultaneously on multiple servers, saving even more time.
With the PowerShell scripts, which you will also learn more about later, you can repeatedly perform repetitive tasks. One of PowerShell's biggest benefits is its ability to help you automate tasks, from rolling out new servers in virtual environments to configuring new Microsoft 365 mailboxes and everything that comes in between.
At their simplest, PowerShell scripts are nothing more than a series of commands, making it easy to transition from individual commands in a CLI to automated scripts.
What Can You Do with PowerShell?
N ow you know what PowerShell is, lets look at what you can do with it.
First, keep it in mind that PowerShell is not one of those technologies that will disappear as fast as it appeared its here to stay. Although we moved on from CLIs and green screens, adopting GUIs for just about everything, we are going back to the old CLI methods and there are good reasons, the primary one regarding development lifecycles.
Next page