0

I have another issue just whipping me. I borrowed the following code (I can't recall from where) that looked super promising for determining phone numbers present in all files in a file share. The code absolutely makes sense to me, and that is why I am confused why it is not working. I test it with a document that I created in which I placed a phone number. It returned nothing. Any help would be greatly appreciated.

foreach ($file in Get-ChildItem -Recurse  | Select-String -pattern '^\d{3}-\d{3}-\d{4}' | Select-Object -Unique Path) {$file.path}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Athanasius12
  • 37
  • 1
  • 6

1 Answers1

1

The problem is most likely that the

^\d{3}-\d{3}-\d{4}

regex requires the phone number to be first characters in a document (or a line) while in the actual documents it may be somewhere in the middle.

Change the ^ anchor to \b (word boundary). You may also want to add this word boundary anchor after the potential phone number:

\b\d{3}-\d{3}-\d{4}\b
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40