PowerShell has a range operator. I you want to express the numbers from 1 to 5, you can specify that as 1..5. 1..5 actually build an array (object[]). One question I had during training was: Can the range operator be used on characters? The simple answer is no, but you can achieve the same with a small work around. Say you want to check the drive letters between F and M -
[char]"f"..[char]"m" | Foreach-Object { "Checking " + [char]$_ }
if the numbers are not in a sequence, you can use on of these -
"fgmr".toCharArray() | Foreach-Object { "Checking " + [char]$_ }
"fgmr".getEnumerator() | Foreach-Object { "Checking " + [char]$_ }
"f","g","m","r" | Foreach-Object { "Checking " + [char]$_ }
BTW: The range operator is limited to 50,000 elements - try 1..60000 and see the error message. If you need to go from 1 to 100,000, you can either do a for statement -
for($i=1;$i -le 100000;$i++) { blah }
or
1..50000+50001..100000 | { blah }
Got inspired by Oisin / Nivot Ink.
No comments:
Post a Comment