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

PowerShell: Find Guest VMs Associated with a Certain Storage Path

# findGuestMvsByStorage.ps1 $storagePath='\\SMBSERVER009' function getAllGuestVms($clusterName){ try{ Import-Module FailoverClusters $clusterName=if($clusterName){ $clustername }else{ (get-cluster).name } $allHyperVHosts={(Get-ClusterNode -Cluster…

PowerShell: Check IP Conflicts of Computers in Active Directory

We have ran into issues where a group of virtual machines living on a DHCP…

Quick & Useful Snippet to Set SSL TLS Protocol of PowerShell

$requiredTls='Tls12' $availableSslProtocols=[enum]::getnames([net.securityprotocoltype]) if([Net.ServicePointManager]::SecurityProtocol -notin $requiredTls -and $requiredTls -in $availableSslProtocols){ [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::$requiredTls }