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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server Automation with PowerShell Cookbook, Fifth Edition

You're reading from   Windows Server Automation with PowerShell Cookbook, Fifth Edition Powerful ways to automate, manage, and administrate Windows Server 2022 using PowerShell 7.2

Arrow left icon
Product type Paperback
Published in Jan 2023
Publisher Packt
ISBN-13 9781804614235
Length 714 pages
Edition 5th Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Thomas Lee Thomas Lee
Author Profile Icon Thomas Lee
Thomas Lee
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Installing and Configuring PowerShell 7 FREE CHAPTER 2. Managing PowerShell 7 in the Enterprise 3. Exploring .NET 4. Managing Active Directory 5. Managing Networking 6. Implementing Enterprise Security 7. Managing Storage 8. Managing Shared Data 9. Managing Printing 10. Exploring Windows Containers 11. Managing Hyper-V 12. Debugging and Troubleshooting Windows Server 13. Managing Windows Server with Window Management Instrumentation (WMI) 14. Managing Windows Update Services 15. Other Books You May Enjoy
16. Index

Exploring PSReadLine

Early versions of PowerShell were monochrome, although the terminal (conhost.exe) did provide some limited coloring. These versions of Windows PowerShell also lacked some of the cool features found in Linux shells.

With PowerShell 4, PowerShell included a new module, PSReadLine. The module provides a command-line editing experience that is on a par with the best of the Linux command shells (e.g., BASH). The PSReadLine module provides additional console editing features within both PowerShell 7 and Windows PowerShell.

When you type into a PowerShell console, PSReadLine intercepts your keystrokes to provide syntax coloring, simple syntax error notification, etc. PSReadLine enables you to customize your environment to suit your personal preferences. Some key features of the module include:

  • Syntax coloring of the command-line entries
  • Multiline editing
  • History management
  • Customizable key bindings
  • Highly customizable

For an overview of PSReadLine, see https://learn.microsoft.com/powershell/module/psreadline/about/about_psreadline. And for more details, you can view the PSReadLine’s GitHub README file: https://github.com/PowerShell/PSReadLine/blob/master/README.md.

There are several minor issues you may need to understand. One issue is the naming of this module. The original name of the module was PSReadline. At some point, the module’s developers changed the module’s name to PSReadLine (capitalizing the L character in the module name). Unfortunately, that change caused Update-Help to fail since there is case sensitivity in module names). You can fix this by manually updating the module’s folder name from PSReadline to PSReadLine.

Another issue arises if you use VS Code. The PSReadLine module ships natively with PowerShell 7. If you use VS Code’s PowerShell Integrated Terminal, you cannot load any newer version of PSReadline. At least until the development team updates the PowerShell extension to utilize the updated version of PSReadLine. This is by design. For most IT pros, this probably does not matter much. But you may find later versions of PSReadLine contains features you want – if so, you should be able to use the Preview (i.e., beta!) version of the PowerShell extension, which supports the latest version of PSReadLine.

A final issue relates to changes made at V2. With the module’s V2 release, the dev team made some changes that were not backward compatible. But be aware that some older scripts may need adjusting. Many blog articles, for example, use the older V1 syntax for Set-PSReadLineOption, which fails with later versions of the module. You may still see the old syntax if you use your search engine to discover examples. Likewise, some of the examples in this recipe fail should you run them utilizing PSReadline V1. Over time, though, the documentation and blog posts should catch up.

You run this recipe on SRV1 after you have installed PowerShell 7. Run this recipe in VS Code after configuring VS Code and loading the Cascadia Code font.

How to do it...

  1. Getting commands in the PSReadline module
    Get-Command -Module PSReadLine
    
  2. Getting the first 10 PSReadLine key handlers
    Get-PSReadLineKeyHandler |
      Select-Object -First 10
        Sort-Object -Property Key |
          Format-Table -Property Key, Function, Description
    
  3. Discovering a count of unbound key handlers
    $Unbound = (Get-PSReadLineKeyHandler -Unbound).count
    "$Unbound unbound key handlers"
    
  4. Getting the PSReadline options
    Get-PSReadLineOption
    
  5. Determining the VS Code theme name
    $Path       = $Env:APPDATA
    $CP         = '\Code\User\Settings.json'
    $JsonConfig = Join-Path  $Path -ChildPath $CP
    $ConfigJSON = Get-Content $JsonConfig
    $Theme = $ConfigJson |
               ConvertFrom-Json |
                 Select-Object -ExpandProperty 'workbench.colorTheme'
    
  6. Changing the VS Code colors
    If ($Theme -eq 'Visual Studio Light') {
      Set-PSReadLineOption -Colors @{
        Member    = "'e[33m"
        Number    = "'e[34m"
        Parameter = "'e[35m"
        Command   = "'e[34m"
      }
    }
    

How it works...

In step 1, you use Get-Command to discover commands in the PSReadLine module, with

Figure 1.35: Discovering commands in the PSReadLine module

In step 2, you use Get-PSReadLineKeyHandler to discover some of the key handlers implemented by PSReadline, with output like this:

Figure 1.36: Viewing ten PSReadLine key handlers

In step 3, you calculate how many key handers are unbound and are available for you to use. The output from this step is:

Figure 1.37: Counting unbound key handlers

PSReadLine has many options you can set. In step 4, you use the Get-PSReadLineOption command to view the option settings, with output like this:

Figure 1.38: Counting unbound key handlers

In step 5, you determine the current VS Code theme, and in step 6, you change the PowerShell token colors, but only if the theme set is the Visual Studio Light theme. These two steps produce no console output.

There’s more...

In step 1, you open a new Windows PowerShell console. Make sure you run the console as the local administrator.

In step 3, you view the first ten of the PSReadLine’s key handlers. Using PowerShell, PSReadLine captures specific keyboard sequences (e.g., Alt + L) and uses an assigned key handler to carry out some action. Typing Alt + L clears the terminal window (in VS Code and the PowerShell console). PSReadline implements a range of key handers with plenty of room for you to customize the editing experience and provide significant customization of the shell. As you can see in step 3, you can use over 100 key combinations to implement your own customizations.

The screenshots throughout most of this book use this color theme. You could extend your profile files to update token colors each time you start a VS Code terminal.

Join our community on Discord

Join our community’s Discord space for discussions with the author and other readers:

https://packt.link/SecNet

You have been reading a chapter from
Windows Server Automation with PowerShell Cookbook, Fifth Edition - Fifth Edition
Published in: Jan 2023
Publisher: Packt
ISBN-13: 9781804614235
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