How to use Get-ChildItem with Regex in one command

I've this command in PowerShell following :

Get-ChildItem  –File  -Name

I've this kind of result :

50050100014_preview.png
50050100014_thumbnail.png
50050100016_master.png
50050100016_preview.png
50050100016_thumbnail.png
50050100017_master.png
50050100017_preview.png
50050100017_thumbnail.png

I would like to apply this regex "_[a-zA-Z]+.png" for have just the number in the names of files. How to join the Regex in one line of command.


Solution 1:

You will simply need to replace anything in the string that matches the regex. you can use the -replace operator for that.

(Get-ChildItem -File -Name) -replace '_[a-zA-Z]+.png'

Example output:

PS C:\xy> Get-ChildItem -File -Name
50050100014_preview.png
50050100014_thumbnail.png
50050100016_master.png

PS C:\xy> (Get-ChildItem -File -Name) -replace '_[a-zA-Z]+.png'
50050100014
50050100014
50050100016