Posted On March 7, 2022

PowerShell: Search File Contents Matching a Phrase or String

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Search File Contents Matching a Phrase or String
$searchDirectories='C:\users\Public\.jenkins\jobs'
$searchDepth=1
$fileNameScope='*.xml'
$searchString='kimconnect'

# Select the files and perform the search in each file
$files=Get-ChildItem -Path $searchDirectories -Depth $searchDepth -Filter $fileNameScope
$files|%{
    $x=Select-String -Path $_ -Pattern $searchString;
    if($x){
        write-host "File $_ contains $searchString`r`n$x`r`n"
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Bash Shell: Rename Files – Prepend and Append

Pre-pending # Prepend any file that doesn't have the word "/thumbs_" in its full path…

Basic HTML and HTML5: Link to External Pages with Anchor Elements

<h2>CatPhotoApp</h2><main><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."><p>Kitty ipsum dolor sit amet, shed…

PowerShell: Error While Invoking Functions Containing 2 Parameters

Issue: This function will raise an error when being invoked: function localFunc ($x, $y){ begin{}…