Powershell loop with numbers to alphabet
12,473
Solution 1
You can sum your loop index with 65. So, it'll be: 0 + 65 = A, 1 + 65 = B ...
for ($test = 0; $test -lt 26; $test++)
{
[char](65 + $test)
}
Solution 2
PS2 to PS5:
97..(97+25) | % { [char]$_ }
Faster:
(97..(97+25)).ForEach({ [char]$_ })
PS6+:
'a'..'z' | % { $_ }
Faster:
('a'..'z').ForEach({ $_ })
Author by
vnavna
Updated on June 14, 2022Comments
-
vnavna almost 2 years
I need help with the following: Create a for loop based on the conditions that the index is initialized to 0, $test is less than 26, and the index is incremented by 1 For each iteration, print the current letter of the alphabet. Start at the letter A. Thus, for each iteration, a single letter is printed on a separate line. I am not able to increment the char each time the loop runs
for ($test = 0; $test -lt 26; $test++) { [char]65 }
I have tried multiple attempts with trying to increment the char 65 through 90 with no success. Is there an easier way to increment the alphabet to show a letter for each loop that is ran?