Starting with a side note:
Don't use $Input
as a custom variable as it is a preserved automatic variable
For what you are trying:
You iterating trough an array and check whether each item (with will automatically type cast to a string) is -like
a 1
preceded by any number of characters and succeeded by any number of characters which is either true or false (and not the total number of ones in the string).
Instead
You might want to use the Select-String
cmdlet with the -AllMatches
switch which counts all the matches:
[array]$inputs = 81,11,101,1811,1981
$count = 0
foreach($i in $inputs)
{
Write-Host "Processing element $input"
$count += ($i | Select-String 1 -AllMatches).Matches.Count
}
Write-Host "Number of 1's in the given array is $count"
In fact, thanks to the PowerShell member enumeration feature, you do not even have to iterate through each array item for this, and just simplify it to this:
[array]$inputs = 81,11,101,1811,1981
$count = ($Inputs | Select-String 1 -AllMatches).Matches.Count
Write-Host "Number of 1's in the given array is $count"
Number of 1's in the given array is 10