Use regex to find string in filenames and output to text file

Sorry for the newb question. I’m new to PowerShell.

Is there a way I can get the string of data between XYZ and the first . symbol and output it to a text file?

$files=Get-ChildItem *.pdf | SelectObject -ExpandProperty Name

PS C:\Users\Owner\Documents> echo $files
XYZ0534.7.02.19cc.pdf
XYZ0568.7.03.19cc.pdf
XYZ0569.7.03.19cc.pdf
XYZ0975.7.01.19cc.pdf
XYZ0988.7.02.19cc.pdf
XYZ1051.7.01.19cc.pdf
XYZ1094.7.02.19cc.pdf
XYZ1105.7.03.19cc.pdf
XYZ1460.7.02.19ccCATS only.pdf
XYZ1481.7.01.19cc.pdf
XYZ1505.7.03.19cc.pdf
XYZ1902.7.02.19cc.pdf
XYZ2225.7.03.19cc.pdf
XYZ2455.7.03.19ccCATS only.pdf
XYZ2539.7.01.19cc.pdf
XYZ2755.7.01.19cc.pdf
XYZ2798.7.02.19cc.pdf
XYZ2833.7.02.19cc.pdf
XYZ2910.7.01.19cc.pdf
XYZ2949.7.03.19cc.pdf
XYZ3018.7.01.19cc.pdf
XYZ3042.7.02.19cc.pdf
XYZ32001.7.03.19ccCATS only.pdf
XYZ60010.7.03.19cc.pdf
XYZ60013.7.02.19cc.pdf

 

I have the following regex that works for matching the string between XYZ and the first . symbol but not sure how to achieve my end result.

(?<=XYZ)([^.]+)

May be

Get-ChildItem *.pdf | ForEach-Object -Process { $_.BaseName -match 'XYZ([^.]+)' ; $Matches[1] }

-match is a regex operator and here the relevant match is capture using a capture group. The capture group result and the match will be in $Matches automatic variable. $Matches[0] will be the match and $Matches[1] is the capture value.

That works beautifully! However, is there any way to omit “TRUE” from the results?

 

UPDATE: Never mind… I found the following which explains why the results returns TRUE.

https://stackoverflow.com/questions/27194148/powershell-match-in-function-gets-extra-true-false-when-returned

… and I omitted them by modifying your code to:

Get-ChildItem *.pdf | ForEach-Object -Process { $_.BaseName -match 'XYZ([^.]+)' > $null ; $Matches[1] }

 

Thank you so much for your help!