1.6 KiB
1.6 KiB
Windows PowerShell Select-String Sample
This sample creates a cmdlet called Select-Str that searches files for specified patterns. The patterns can be case-sensitive or case-insensitive. The user can also specify a script block to use for performing the matching operation instead of relying on the cmdlet's own logic which uses regular expressions for matching.
Sample Objectives
This sample demonstrates the following:
- Using PSPaths with cmdlet parameters.
- Using script blocks with cmdlet parameters.
- Using session state in a cmdlet implementation.
Related topics
Operating system requirements
Client
Windows 8.1
Server
Windows Server 2012 R2
Build the sample
-
Start Microsoft Visual Studio and select File > Open > Project/Solution.
-
Go to the directory named for the sample, and double-click the Visual Studio Solution (.sln) file.
-
Press F7 or use Build > Build Solution to build the sample.
The library will be built in the default \bin or \bin\Debug directory.
Run the sample
- Store the assembly in the following module folder: [user]/Documents/WindowsPowerShell/Modules/SelectStrCommandSample
- Start Windows PowerShell.
- Run the following command: Import-Module SelectStrCommandSample (This command loads the assembly into Windows PowerShell.)
- Type Get-Command Select-Str -Syntax to see the syntax for the Select-Str cmdlet.
- Type Select-Str to run the cmdlet.