Reader small image

You're reading from  Mastering Windows Server 2019, Third Edition - Third Edition

Product typeBook
Published inJul 2021
PublisherPackt
ISBN-139781801078313
Edition3rd Edition
Concepts
Right arrow
Author (1)
Jordan Krause
Jordan Krause
author image
Jordan Krause

Jordan Krause has been an IT professional for more than 20 years and has received 9 Microsoft MVP awards for his work with Microsoft server and networking technologies. One of the world's first experts on Microsoft DirectAccess, he has a passion for helping companies find the best ways to enable a remote workforce. Committed to continuous learning, Jordan holds certifications as an MCSE, MCSA, and MCITP Enterprise Administrator, and has authored numerous books on Microsoft technologies. Jordan lives in beautiful West Michigan (USA), but works daily with companies around the world.
Read more about Jordan Krause

Right arrow

PowerShell

Let's be honest, many of us are still using Command Prompt on a daily basis. If you have cut over and are using the newer PowerShell prompt as a total replacement for Command Prompt, I applaud you! I, however, still tend to open up cmd.exe as a matter of habit, though with the most recent releases of Windows 10 and Windows Server 2019, I am definitely making a more conscious effort to use the newer, bluer, prettier, and more powerful interface that is PowerShell. In this chapter, we are going to explore some of the reasons that you should do so too. Other than the fact that Microsoft seems to have shrunk the default text size in Command Prompt to deter us from using it, which I find pretty funny, we are going to take a look at some of the technical reasons that PowerShell is far and away more useful and powerful than Command Prompt could ever dream of being.

In this chapter, we will cover the following topics:

  • Why move to PowerShell?
  • Working within...

Why move to PowerShell?

I don't think there is any question in people's minds that PowerShell is indeed the evolution of Command Prompt, but the reason that many of us still default to the old interface is that it still contains all of the functionality required to accomplish what we historically have needed to do on our servers. What Command Prompt really contains is the ability to do the same things that we have always done from Command Prompt, and nothing else. Without realizing it, there are a lot of functions that you use the GUI to accomplish that cannot be done well from within a Command Prompt window.

The limitations within Command Prompt that force you into using your mouse to interface with the GUI do not exist with PowerShell. It is fully comprehensive and capable of modifying almost any aspect of the Windows operating system. How did PowerShell become so much more powerful than Command Prompt? It differs from any classic I/O shell in that it is built on top...

Working within PowerShell

The first step to doing real work with PowerShell is getting comfortable interfacing with the platform and becoming familiar with the daily routines of working from this command-line interface, rather than relying on your mouse pointer. Here, we will explore some of the most common ways that I have seen server administrators make use of PowerShell to enhance their daily workflow.

We will be working throughout this chapter inside the version of PowerShell that comes out of the box with Windows Server 2019, PowerShell 5.1. You may have heard the news of PowerShell 7's release. If you are familiar with this and already utilizing it, everything that we discuss will work perfectly well on that updated platform. To administer a Windows Server instance, the differences between versions won't have much of a bearing. Installing PowerShell 7 will not update PowerShell 5.1, but rather they will run side by side.

PowerShell 7 is open-source and comes...

Using a pipeline

Following the last couple of example cmdlets that we ran, you may be thinking to yourself, "I see that he is using that vertical line on the key above Enter on my keyboard, but why?"

Great question. In Command Prompt, we generally issue one command at a time. The same is often true for PowerShell when we are manually interacting with it, but in PowerShell, we have the potential for so much more power. One of those items of power is the ability to create a pipeline of commands. In other words, you can connect, or chain, commands together. This is commonly referred to as piping information from one cmdlet to another cmdlet and is done by using that little |.

Cmdlets often output data. If you then want to utilize that set of data against another cmdlet, this is where the pipe comes in handy. In our last example command, we told PowerShell to gather all of the commands that included the word Restart by performing Get-Command -Name *Restart*. Then we...

PowerShell Integrated Scripting Environment

Most server administrators are familiar with the concept of creating batch files for use in the Command Prompt world. Have a series of commands that you want to run in sequence? Need to run this sequence of commands multiple times across different servers or over and over again in the future? Throwing multiple commands inside a text document and then saving it with the .BAT file extension will result in a batch file that can be run on any Windows computer, issuing those commands in sequence, which saves you the time and effort of having to plunk out these commands over and over inside the command-line interface.

Scripting in PowerShell is the same idea but is much more powerful. Commands in Command Prompt are useful, but limited, while PowerShell cmdlets have the ability to manipulate anything within the operating system. With PowerShell, we can reference items from inside environment variables or the registry, we can easily issue commands...

Remotely managing a server

Now that we have worked a little bit in the local instance of PowerShell and have explored a couple of methods that can be used to start creating scripts, it is time to take a closer look at how PowerShell fits into your centralized administration needs. If you start using PowerShell for server administration but are still RDPing into the servers and then opening PowerShell from there, you're doing it wrong. We already know that you can tap remote servers into Server Manager so that they can be managed centrally. We also know that the tools inside Server Manager are, for the most part, just issuing a series of PowerShell cmdlets when you click on the buttons. Combine those two pieces of information, and you can surmise that PowerShell commands and cmdlets can be easily run against remote systems, including ones that you are not currently logged in to.

Taking this idea and running with it, we are going to look over the criteria necessary to make...

Desired State Configuration

There is some powerful functionality in the more recent versions of PowerShell, provided by something called Desired State Configuration (DSC). DSC is a management platform plugged into PowerShell, which provides some new functions and cmdlets that you can take advantage of in your scripts to enable some really cool features. As the name implies, it allows you to build configurations inside PowerShell that will provide a desired state. What do I mean by that? Well, in a basic sense, DSC makes sure that the PowerShell scripts you build will always work the same way across all of the servers where you apply them by making sure the servers themselves are configured in the same way. It is quite easy to build a script in a way that means it will work correctly on the server you are currently working on. But, if you try to roll that same script out to a different server that might reside in a different organizational unit (OU), or have different items installed...

Summary

In Windows Server 2019, we see in multiple places that administration via PowerShell is the recommended path for interacting with our servers. Because the management GUIs are now just shells running PowerShell scripts and the default installation option for Windows Server is Server Core, we can assume that headless, command line-oriented servers are expected to be our servers of the future. Even though PowerShell has been at the core of our operating system functionality since Server 2012, I believe that so far PowerShell has been viewed by most admins as simply an alternative way of managing servers. "Yeah, I know it exists and that I should start using it, and the scripting looks pretty cool, but I can still do anything I want to with the old Command Prompt or my mouse button." That old mentality is quickly changing.

Now that we are experiencing an onset of new technologies, such as DSC, we can see that PowerShell is starting to develop functionality that simply...

Questions

  1. What is the fastest way to get from Command Prompt into PowerShell?
  2. What is the cmdlet that will display all available PowerShell cmdlets?
  3. What PowerShell cmdlet can be used to connect your PowerShell prompt to a remote computer?
  4. What file extension does a PowerShell scripting file have?
  5. To which setting is the DEP configured on a fresh Windows Server 2019 instance?
  6. What key on your keyboard can be used to auto-populate the remainder of a cmdlet or filename when working in a PowerShell prompt?
  7. Which service must be running on a system before it can be connected to by a remote PowerShell connection?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Windows Server 2019, Third Edition - Third Edition
Published in: Jul 2021Publisher: PacktISBN-13: 9781801078313
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Jordan Krause

Jordan Krause has been an IT professional for more than 20 years and has received 9 Microsoft MVP awards for his work with Microsoft server and networking technologies. One of the world's first experts on Microsoft DirectAccess, he has a passion for helping companies find the best ways to enable a remote workforce. Committed to continuous learning, Jordan holds certifications as an MCSE, MCSA, and MCITP Enterprise Administrator, and has authored numerous books on Microsoft technologies. Jordan lives in beautiful West Michigan (USA), but works daily with companies around the world.
Read more about Jordan Krause