Using PowerShell to accomplish any function in Windows Server
As you may have seen earlier in this chapter, an incredibly powerful tool in Windows Server 2019 is PowerShell. Think of PowerShell as a Command Prompt on steroids. It is a command-line interface from which you can manipulate almost anything inside Windows. Better yet, any task that you may wish to accomplish can be scripted out in PowerShell and saved as a .ps1
script file so that you can automate large tasks and schedule them for later or at regular intervals. In this recipe, we'll open up PowerShell and run some sample commands and tasks just to get a quick feel for the interface. In most subsequent chapters of this book, we will do some more specific tasks with PowerShell to go even deeper into the technology.
Getting ready
To start using PowerShell, all you need is a server with Windows Server 2019 installed. PowerShell is installed and enabled by default.
How to do it…
To get a feel for using PowerShell, perform the following steps:
- Our first step to working in PowerShell is finding it. Thankfully, Microsoft has begun the process of phasing out the Command Prompt and PowerShell is now accessible in more places than ever before. The most common methods are pressing WinKey + X and choosing Windows PowerShell (Admin), or typing
PowerShell
into the Start menu, right-clicking it, and choosing Run as administrator: - Test out some commands that you are familiar with from using the Command Prompt, such as
dir
andcls
. These still work! Since you are able to make use of these familiar commands, PowerShell can really be your one and only command-line interface. - Now, let's try some of PowerShell's secret sauce; that is, one of its cmdlets. These are special commands that are built into Windows and allow us to do all kinds of information gathering, as well as manipulate server components. We will be using many of these commands later on. Let's start by pulling some data. Maybe take a look at what IP addresses are on the system with
Get-NetIPAddress
: - The previous command probably gave you a lot more information than you needed, since a lot of companies don't make use of IPv6 inside their network yet. Let's whittle this information down to the IPv4-specific information that you are most likely interested in. Enter
Get-NetIPAddress -AddressFamily IPv4
to attain it: - That's more useful! But if you have a lot of data, this would be very difficult to read. PowerShell supports what's called "pipelining" of requests. This means that the output of one command can be directly sent into a second command. PowerShell comes with commands for making output more human readable, and we can use them with pipelining. One very useful command that I use every day is
Format-Table
. TypeGet-NetIPAddress | Format-Table
into PowerShell:
Tip
PowerShell has a lot of shorthand aliases, which makes typing out commands much easier. For example, you can just use ft
instead of typing out all of Format-Table
. You can get a list of aliases by running Get-Alias
.
How it works…
We just got a feel for some of the many PowerShell commands and cmdlets by launching the program and pulling some data with this particular recipe. There are thousands of Get
commands that can be used to query information from the server and, as you have seen, those cmdlets have various parameters that can be appended to the cmdlets to pull more specific data to meet your needs. To make things even better, it also gives us Set
cmdlets, which will allow us to make use of the PowerShell prompt to configure many aspects of the configuration on our server, as well as remote servers. Almost every recipe in this book from here on will also show you how to do what we'll be covering in PowerShell.