Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server 2019 Cookbook - Second Edition

You're reading from  Windows Server 2019 Cookbook - Second Edition

Product type Book
Published in Jul 2020
Publisher Packt
ISBN-13 9781838987190
Pages 650 pages
Edition 2nd Edition
Languages
Concepts
Authors (2):
Mark Henderson Mark Henderson
Profile icon Mark Henderson
Jordan Krause Jordan Krause
Profile icon Jordan Krause
View More author details
Toc

Table of Contents (18) Chapters close

Preface 1. Chapter 1: Learning the Interface 2. Chapter 2: Core Infrastructure Tasks 3. Chapter 3: Networking 4. Chapter 4: Working with Certificates 5. Chapter 5: Internet Information Services 6. Chapter 6: Remote Access 7. Chapter 7: Remote Desktop Services 8. Chapter 8: Monitoring and Backup 9. Chapter 9: System Insights 10. Chapter 10: Group Policy 11. Chapter 11: File Services and Data Control 12. Chapter 12: Server Core 13. Chapter 13: Working with Hyper-V 14. Chapter 14: Containers and Docker 15. Chapter 15: Desired State Configuration and Automation 16. Chapter 16: Hardening Your Infrastructure 17. Other Books You May Enjoy

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:

  1. 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:
    Figure 1.13 – Searching in the Start menu for PowerShell

    Figure 1.13 – Searching in the Start menu for PowerShell

  2. Test out some commands that you are familiar with from using the Command Prompt, such as dir and cls. 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.
  3. 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:
    Figure 1.14 – Using Get-NetIPAddress to look at IP addresses on a system

    Figure 1.14 – Using Get-NetIPAddress to look at IP addresses on a system

  4. 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:
    Figure 1.15 – Refining your IP address search using –AddressFamily IPv4

    Figure 1.15 – Refining your IP address search using –AddressFamily IPv4

  5. 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. Type Get-NetIPAddress | Format-Table into PowerShell:
Figure 1.16 – Using Format-Table to organize an output

Figure 1.16 – Using Format-Table to organize an output

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.

lock icon The rest of the chapter is locked
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.
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}