带数字到字母表的 Powershell 循环

Powershell loop with numbers to alphabet

我需要以下方面的帮助: 根据index初始化为0,$test小于26,index自增1的条件创建for循环 对于每次迭代,打印字母表中的当前字母。从字母 A 开始。因此,对于每次迭代,单个字母打印在单独的行上。 每次循环运行时我都无法增加 char

for ($test = 0; $test -lt 26; $test++)
{
[char]65
}

我曾多次尝试将字符 65 增加到 90,但均未成功。 有没有更简单的方法来增加字母表以显示每个循环的字母 运行?

您可以将循环索引与 65 相加。因此,它将是:0 + 65 = A,1 + 65 = B ...

for ($test = 0; $test -lt 26; $test++)
{
    [char](65 + $test)
}

PS2 到 PS5:

97..(97+25) | % { [char]$_ }

更快

(97..(97+25)).ForEach({ [char]$_ })

PS6+:

'a'..'z' | % { $_ }

更快:

('a'..'z').ForEach({ $_ })

以下示例不假定 'A' 为 65,并且还允许您将其更改为您想要的任何启动驱动器。例如,从 'C' 开始并转到 'Z':

$start = 'C'
for ($next = 0; $next -lt (26 + [byte][char]'A' - [byte][char]$start); $next++) {
    [char]([byte][char]$start + $next)
}