I am struggling to find a way to get only the first set of numbers in a file name in PowerShell. The file names can be similar to the ones below but I only want to get the first string of numbers and nothing else.
Example file names:
123456 (12).csv
123456abc.csv
123456(Copy 1).csv
123456 (Copy 1).csv
What I am currently attempting:
$test = "123456 (12).csv"
$POPieces = $test -match "^[0-9\s]+$"
Write-Host $POPieces
What I'd expect from above:
123456
$test -match '^\d+'; $matches[0]$anchors it to the end,^anchors to the beginning, so you're telling powershell to match a regex that has one or more numbers or spaces, and nothing else.