Skip to content

Instantly share code, notes, and snippets.

@reedjones
Created July 17, 2024 17:58
Show Gist options
  • Save reedjones/d757541bf1c4ae188f922090eae27624 to your computer and use it in GitHub Desktop.
Save reedjones/d757541bf1c4ae188f922090eae27624 to your computer and use it in GitHub Desktop.
powershell Get files that match the inclusion and exclusion paths and filename
# Search-FilesContent.ps1
param (
[string]$IncludePath = "*",
[string]$ExcludePath = "*node_modules*",
[string]$Filename = "*",
[string[]]$IncludeContent = @(),
[string[]]$ExcludeContent = @()
)
# Get files that match the inclusion and exclusion paths and filename
$files = Get-ChildItem -Recurse -Path $IncludePath -Include $Filename -Exclude $ExcludePath -File
foreach ($file in $files) {
$content = Get-Content $file.FullName
# Check if file content includes all specified words
$includeMatch = $true
foreach ($word in $IncludeContent) {
if ($content -notmatch $word) {
$includeMatch = $false
break
}
}
# Check if file content excludes any specified words
$excludeMatch = $false
foreach ($word in $ExcludeContent) {
if ($content -match $word) {
$excludeMatch = $true
break
}
}
# Output the file path if it matches the criteria
if ($includeMatch -and -not $excludeMatch) {
Write-Output $file.FullName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment