Powershell Extract Number from File Name
问题 Files names could be: 1234_billing.txt 1234billling.txt 123_billing.txt 123billing.txt How can I extract the only the number in all 4 cases? I've tried -split and $_.BaseName.Substring() but can't seem to get it correct. 回答1: Assuming that the filenames are in the array variable $flist , the following will do the trick: foreach ($file in $flist) { if ($file -match "\d+") { $matches.value } } The -match operator takes as its right operand a regex pattern; in this case we use the pattern \d+ to