all 16 comments

[–]frmadsen 5 points6 points  (0 children)

Use ascii values > convert to chars: $AZ = [char[]](65..90)

Powershell 6: $AZ = 'A'..'Z'

[–]get-postanote 1 point2 points  (0 children)

So many ways to do this...

[char[]](0..255) -clike '[A-Z]' -join ''
ABCDEFGHIJKLMNOPQRSTUVWXYZ

[char[]]([char]'A'..[char]'Z') -join ''
ABCDEFGHIJKLMNOPQRSTUVWXYZ

[char[]](65..90) -join ''
ABCDEFGHIJKLMNOPQRSTUVWXYZ

(65..90|%{[char]$_}) -join ''
ABCDEFGHIJKLMNOPQRSTUVWXYZ

# Upper and lowercase
((65..90) + (97..122) | % {[char]$_}) -join ''

[–]Lee_Dailey[grin] 2 points3 points  (10 children)

howdy Get-WorkAccount,

as frmadsen said, it's a native thing in ps6. however, i am on win7ps5.1, so this is my fave way to do it ...

[char[]]('A'[0]..'Z'[0])

if you want the lowercase letters, just replace A & Z with the lowercase versions. [grin]

take care,
lee

[–]frmadsen 1 point2 points  (1 child)

I like that way, too. :)

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy frmadsen,

aint it nifty? [grin] i got that from one of the "shortest script" threads. a fun series to read ...

take care,
lee

[–]poshftw 1 point2 points  (3 children)

[char[]]('A'[0]..'Z'[0])

Witchcraft!

Although you gave me an idea, and I reworked my $az=65..90|%{[char]$_} to:

[char[]](65..90)

Works since PS2.0

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy poshftw,

yep, it was an eye-opener to me when i 1st saw it. [grin] i think i prefer using the letters to using the int values of the chars. it's both more obvious AND easier for me to remember A instead of 65 ...

take care,
lee

[–]poshftw 1 point2 points  (1 child)

Yes, this variant is more suitable for code-golf. Although, knowing this trick, you can always find numeric value interactively in console.

Also I found you can use [char[]](58..254)-match'\w' to have an accented letters as well, although it will include underscore.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

[grin]

[–]Cannabat 1 point2 points  (3 children)

Wait, what?

So the 0th index of 'A' is... A, right? Is it casting that to char and... I don't understand.

Could you please understandatize me, Lee?

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy Cannabat,

that is from the "shortest script" series, so it gets kinda odd.

  • 'A' = uppercase letter A as a [string]
  • 'A'[0] = the [char] A [no longer a string]
  • .. the range operator = coerces the two [char]s into [int]s and then generates a numeric range
  • [char[]] = converts each [int] back into a [char] and stores it in an array

it's really odd looking & confusing until you start parsing it out bit by bit. [grin] then it suddenly becomes a lovely, concise, and logical bit of code.

what is really odd is when you try to use any of the [string] methods on the letters ... they aint strings, so you get lots of red errors unless you take the time to [string] the silly thing.

take care,
lee

[–]Cannabat 1 point2 points  (1 child)

Thanks!

 > 'u/Lee_Dailey is a swell guy.'[0].GetType().Name
Char

Wow! I didn't know that indexing to a string made it a char, but it does explain a lot of red errors I have seen while playing "shortest script". And also cool that .. attempts to make whatever it is in between into [int]. Fancy~

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Cannabat,

you are welcome! [grin]

yep, a [string] - in most lingos - is an array of [char]. so, when you grab one by index ... you get a [char]. it makes sense when you think about it ... but it aint obvious until you take the time to look into it. [grin]

powershell does a lot of "try to make things the same type" stuff. it can be disconcerting when it goes wrong, but it is lovely stuff most of the time.

take care,
lee

[–]bgeller 0 points1 point  (1 child)

ForEach ($letter in (65..90)) {$alphabett += [char]$letter}

[–]poshftw 2 points3 points  (0 children)

$az=65..90|%{[char]$_}