To complement Sage Pourpre's helpful solutions:
Two more ways to count the number of occurrences of a given character in a string:
# -> 5, 4
@{ Name = 'ABC_2019_02_01_blabla_05.pdf' },
@{ Name = 'ABC_DEF_192_1111_oaoaoa.pdf' } |
ForEach-Object {
($_.Name.ToCharArray() -eq '_').Count
}
This relies on the -eq operator acting as a filter when the LHS is an array: in effect, it returns the subarray of characters that are _, whose element count is therefore the number of _ chars. in the input.
# -> 5, 4
@{ Name = 'ABC_2019_02_01_blabla_05.pdf' }, # Sample input
@{ Name = 'ABC_DEF_192_1111_oaoaoa.pdf' } |
ForEach-Object {
($_.Name -replace '[^_]').Length
}
This relies on using the regex-based -replace operator for removing all characters other than _ ([^_]) from the input string, which leaves a string composed only of the _ chars., whose length is therefore the number of _ chars. in the input.
As for what you tried:
Your attempt, if corrected, has the potential to perform additional validation, such as requiring that _ characters be surrounded by at least one other character, such that, say, 'a_b_c_d_e_f.pdf' is valid, but 'abcdef_____.pdf' is not.
# -> $true, $false, $false
@{ Name = 'ABC_2019_02_01_blabla_05.pdf' }, # OK
@{ Name = 'abcdef_____.pdf' }, # Correct number of "_", but in the wrong places
@{ Name = 'ABC_DEF_192_1111_oaoaoa.pdf' } | # Not enough "_"
ForEach-Object {
$_.Name -match '^([^_]+_){5}[^_]+$'
}
For an explanation of the regex and the ability to experiment with it, see this regex101.com page.