Author Posts

February 16, 2018 at 7:51 pm

So I am trying to pull all 8 and 9 digit sequence from a text file. There is multiple patterns to these sequences, these could be xx-xxx-xxx, xx xxx xxx, xxxxxxxx, xxx-xx-xxxx, xxxxxxxxx, xxx xx xxxx. I came up with this but it is pulling more than just the numbers. Where am I making a mistake?

$Num = @()

$Num += Get-ChildItem -Path "$PSSCRIPTROOT\text.txt" |select-string " [0-9]{3}[-| ][0-9]{2}[-| ][0-9]{4}"
$Num += Get-ChildItem -Path "$PSSCRIPTROOT\text.txt" |select-string " [0-9]{3}[-][0-9]{2}[-][0-9]{4}"
$Num += Get-ChildItem -Path "$PSSCRIPTROOT\text.txt" |select-string " [0-9]{9}"

$Num += Get-ChildItem -Path "$PSSCRIPTROOT\text.txt" |select-string " [0-9]{2}[-| ][0-9]{3}[-| ][0-9]{3}"
$Num += Get-ChildItem -Path "$PSSCRIPTROOT\text.txt" |select-string " [0-9]{2}[-][0-9]{3}[-][0-9]{3}"
$Num += Get-ChildItem -Path "$PSSCRIPTROOT\text.txt" |select-string " [0-9]{8}"

ForEach ($Number in $Num){
#$Number

$Found = $Number.ToString().Split(":")
$o = new-object PSObject
$o | add-member NoteProperty "FoundOnLine" $Found[2]
$o | add-member NoteProperty "Number" $Found[3]
$o | add-member NoteProperty "Number2" $Found[4]
$o | add-member NoteProperty "Number3" $Found[5]

$o | export-csv "$PSscriptroot\FoundNumbers-$((get-date).ToString("M-dd-yyyy")).csv" -notypeinformation -Append -Force
Write-Output $o

}