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.

2

1 Answer

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like