Search icon CANCEL
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 Automation with PowerShell Cookbook

You're reading from   Windows Server Automation with PowerShell Cookbook Powerful ways to automate and manage Windows administrative tasks

Arrow left icon
Product type Paperback
Published in Jul 2021
Publisher Packt
ISBN-13 9781800568457
Length 674 pages
Edition 4th Edition
Languages
Concepts
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 (18) Chapters Close

Preface 1. Installing and Configuring PowerShell 7 2. Introducing PowerShell 7 FREE CHAPTER 3. Exploring Compatibility with Windows PowerShell 4. Using PowerShell 7 in the Enterprise 5. Exploring .NET 6. Managing Active Directory 7. Managing Networking in the Enterprise 8. Implementing Enterprise Security 9. Managing Storage 10. Managing Shared Data 11. Managing Printing 12. Managing Hyper-V 13. Managing Azure 14. Troubleshooting with PowerShell 15. Managing with Windows Management Instrumentation 16. Other Books You May Enjoy
17. Index

Using Select-String

The Select-String command, included with Windows PowerShell, has been improved in PowerShell 7. You use this command to search for strings either inside pipelined objects or within text files. This command is conceptually similar to the grep command in Linux. With PowerShell 7, the PowerShell team has added some excellent new features to this excellent command, which you look at in this recipe.

Getting ready

You run this recipe on SRV1 after you have installed PowerShell 7 and Visual Studio Code, and once you have created a console profile file.

How to do it...

  1. Getting a file of text to work with
    $Source       = 'https://www.gutenberg.org/files/1661/1661-0.txt'
    $Destination  = 'C:\Foo\Sherlock.txt'
    Start-BitsTransfer -Source $Source -Destination $Destination
    
  2. Getting the book's contents
    $Contents = Get-Content -Path $Destination
    
  3. Checking the length of The Adventures of Sherlock Holmes
    "The book is {0} lines long" -f $Contents.Length
    
  4. Searching for "Watson" in the book's contents
    $Match1 = $Contents | Select-String -Pattern 'Watson'
    "Watson is found {0} times" -f $Match1.Count
    
  5. Viewing the first few matches
    $Match1 | Select-Object -First 5
    
  6. Searching for "Dr. Watson" with a regular expression
    $Contents | Select-String -Pattern 'Dr\. Watson'
    
  7. Searching for "Dr. Watson" using a simple match
    $Contents | Select-String -Pattern 'Dr. Watson' -SimpleMatch
    
  8. Viewing the output when searching from files
    Get-ChildItem -Path $Destination |
      Select-String -Pattern 'Dr\. Watson'
    

How it works…

In this recipe, you look at how you can use the Select-String cmdlet included with PowerShell 7. To investigate this cmdlet, you first download a text file.

In step 1, you use the Start-BitsTransfer command to download the text for a book, The Adventures of Sherlock Holmes, by Sir Arthur Conan Doyle, from the Project Gutenberg website. This step produces no output.

In step 2, you get the text from this book, which you store in the $Contents variable. This step produces no output. In step 3, you report the length of the book, which looks like this:

Figure 2.37: Checking the length of the book

In step 4, you search the book's contents to find all occurrences of "Watson", Sherlock Holmes' faithful companion. This results in 81 occurrences, and looks like this:

Figure 2.38: Searching for occurrences of "Watson" in the book

In step 5, you use the Select-Object cmdlet to view the first five times the command finds the term "Watson" in the book's contents, which looks like this:

Figure 2.39: Viewing the first five "Watson" matches in the book

With Select-String, you can specify a regular expression with which to match the contents. In step 6, you specify a regular expression pattern to search for the string "Dr. Watson". The output of this step looks like this:

Figure 2.40: Searching for "Dr. Watson" with a regular expression pattern

As an alternative to using a regular expression to perform searching, Select-String also takes a simple match, as shown in the output of step 7:

Figure 2.41: Searching for "Dr. Watson" using a simple match

In the previous steps, you have used Select-String to search for the contents of a variable. Another valuable feature of Select-String is the ability to search for text in a file or even multiple files. You can see this in step 8, the output of which looks like this:

Figure 2.42: Searching for "Dr. Watson" in the Sherlock.txt file

There's more...

In steps 1 and 2, you download a text file from Project Gutenberg, a free internet library of eBooks. This site contains a large number of free books in a variety of formats, including basic text. To find more free eBooks, visit the home page at https://www.gutenberg.org/, and to read more about the project, see https://www.gutenberg.org/about/.

An essential improvement to Search-String in PowerShell 7 is the highlighting of the selected string, as you can see in the outputs of steps 5, 6, 7, and 8. From the command line, this makes viewing the output from Select-String much easier to consume. Also, the ability to search across multiple files, as shown in step 8, makes the Select-String cmdlet even more useful.

You have been reading a chapter from
Windows Server Automation with PowerShell Cookbook - Fourth Edition
Published in: Jul 2021
Publisher: Packt
ISBN-13: 9781800568457
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 €18.99/month. Cancel anytime