I am trying to make powershell scripts for search all documents and txt files to find Password pattern. Actually my code is working fine. My regex is capture password but it brings all lines.
Regex Pattern
""" .(?=.{8,20})(?=.[a-z])(?=.[A-Z])(?=.[0-9])(?=.[!^#+$%&{()=}?-|¨@.,:;,]).* """
My regex bring all Line. How can fix this error.
PS Code
$Path = "c:\users\XXXXXXX"
$output_file = ‘C:\Users\XXXXXXXX\Desktop\Result.txt’
$ALLWORDS =Get-ChildItem $Path -Recurse -ErrorAction SilentlyContinue
foreach ($WORDS in $ALLWORDS) { #
$Word = New-Object -ComObject Word.Application
$Word.Visible = $false
$regex = ‘\b.*(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!^#+$%&{()=}?*\-|¨@.,:;,]).*\b’
$Doc = $Word.Documents.Open($WORDS.FullName, $false, $true)
$paras = $doc.Paragraphs
echo $paras.count
foreach($para in $paras){
if( $para.range.text -match $regex) {
$1=$matches.Values,$Words.Name | Out-File $output_file -Append
Write-Host $matches.Values, $words.Name
}
}
$Doc.Close()
$Word.Quit()
$Word = $null
}