Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server 2019 Cookbook

You're reading from   Windows Server 2019 Cookbook Over 100 recipes to effectively configure networks, manage security, and administer workloads

Arrow left icon
Product type Paperback
Published in Jul 2020
Publisher Packt
ISBN-13 9781838987190
Length 650 pages
Edition 2nd Edition
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Jordan Krause Jordan Krause
Author Profile Icon Jordan Krause
Jordan Krause
Mark Henderson Mark Henderson
Author Profile Icon Mark Henderson
Mark Henderson
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Chapter 1: Learning the Interface 2. Chapter 2: Core Infrastructure Tasks FREE CHAPTER 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

Building and executing your first PowerShell script

PowerShell is a great interface for acquiring and configure information about our servers. Most of us are familiar with creating some simple old-fashioned batch files that are driven by the Command Prompt, essentially programming out small tasks within these batch files to automate a series of commands. This saves time later as we do not have to type out the commands line by line, especially for common tasks or for items that we need to run during login.

PowerShell has a hugely increased level of functionality in this regard, but the ability to write out multiple lines of PowerShell into a script file is the most basic of them. We can then launch this script file as we would a batch file, automating tasks while taking advantage of the additional features that PowerShell brings to the table over the Command Prompt. These PowerShell scripts are put together inside .ps1 files; let's build a simple one together to get a feel for running these scripts.

Getting ready

Our work with PowerShell today will be accomplished from a Windows Server 2019 machine. PowerShell is installed by default with Windows, and there is nothing further that we need to install.

How to do it…

Follow these steps to build and execute your first PowerShell script:

  1. Open the Start menu, type Windows PowerShell ISE, and click the desktop app to open it. Do not choose the one ending in (x86) – for some reason, Windows thinks this is a good choice. It isn't. You may need to look further down your Start menu to find the correct one. Windows PowerShell ISE is an editor for PowerShell scripts that is much more useful than opening a simple text editor such as Notepad in order to build our script:
    Figure 1.32 – Locating Powershell ISE in the Start menu

    Figure 1.32 – Locating Powershell ISE in the Start menu

  2. Navigate to File | New from the menus in order to open a blank .ps1 script file.
  3. In your first line, type the following:
    Write-Host "Hello! Here is the current date and time:"
  4. From the toolbar menu, click the green arrow that says Run Script. Alternatively, you can simply press the F5 button. When you run the script, the command and output are displayed in the lower portion of the ISE window:
    Figure 1.33 – Using Windows PowerShell ISE

    Figure 1.33 – Using Windows PowerShell ISE

    Cool? Not really. So far, it's pretty lame – it's just reflecting the text that we told it to echo, but it worked. It didn't even actually tell us what the time is. But that is the nice thing about using the ISE editing tool rather than a generic text editor – you have the ability to quickly test run scripts as you make modifications.

  5. Now, let's add some additional lines to our script so that we get the information we are looking for. You can see a list of available commands on the right-hand side of the screen (you may have to switch your Modules to All) if you would like to browse through what is available, but for our example, simply change your script so that it includes the following:
    Write-Host "Hello! Here is the current date and time: $(Get-Date)"
    Write-Host "The name of your computer is: $(hostname)" 
  6. Press the Run Script button again to see the new output:
    Figure 1.34 – Using Windows PowerShell ISE

    Figure 1.34 – Using Windows PowerShell ISE

  7. Now, navigate to File | Save and save your new .ps1 PowerShell script.
  8. Let's test this script by launching it from within a real PowerShell command window. Go to your Start menu and run PowerShell.
  9. Browse to the location where you just saved your script file – you can use the old DOS cd commands for this. Then, launch the script by inputting .\filename. In my case, I used.\GetTime.ps1. Don't forget that you don't need to type the entire filename – in my case, I could just type G and press Tab and have PowerShell auto-complete:
Figure 1.35 – Running the script file

Figure 1.35 – Running the script file

Tip

The PowerShell ISE comes with Windows by default, but you can write PowerShell scripts in any code editor. Visual Studio Code is a free editor provided by Microsoft that is cross-platform and has excellent support for PowerShell scripting. I have written tens of thousands of lines of PowerShell in Visual Studio Code. Check it out on your Windows 10 machine at https://aka.ms/vscode.

How it works…

In this recipe, we created a very simple PowerShell script and saved it on our server for execution. While, in practice, getting time and date information from your server may come faster by using the standalone Get-Date cmdlet, we provided this recipe to give you a small taste of the ISE and to get your scripting juices flowing. Expanding upon the ideas presented here will start to save you valuable time and keystrokes as you identify more and more ways to automate the tasks and information gathering processes that are part of your daily routines. The possibilities of PowerShell are practically limitless, so make sure that you open it up and start becoming familiar with the interfaces and tools associated with it right away!

Tip

Windows Server 2019 comes with PowerShell 5, which only works on Windows. However, if you upgrade to PowerShell 6 or 7, these run on Linux and OSX as well – so if you wanted to, you can have PowerShell on all your servers, not just your Windows ones! Be careful, though: if you have existing scripts, some things have changed in PowerShell 6 and above, so you will want to check that they still work. With Visual Studio Code and PowerShell 6 or above, you can write and run PowerShell from Windows, Mac, or Linux.

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 $19.99/month. Cancel anytime
Banner background image