I have a Powershell file myfile.ps1:
function Do-It
{
$items = # A command that returns a collection of strings like:
# Element 12657 - <Description trext>
# Element 12656 - <Description trext>
# Element 12655 - <Description trext>
# ...
$pattern = 'Element\s(\d*).*';
foreach ($item in $items) {
$res = $item -match $pattern;
$len = $matches.Length;
$id = $matches[0];
Write-Output "$len $id";
}
}
The problem is that my output is:
1 Element 12657 - <Description trext>
1 Element 12656 - <Description trext>
1 Element 12655 - <Description trext>
...
So no match found. However, if I execute this from cmd, then I get results.
What am I doing wrong? Need to escape something? Thanks
$matchesisHashtable.Hashtabledoes not haveLengthproperty. For PowerShell V3+, auto propertyLengthwill return 1 regardless of content of hash table.