Convert a string to a character array

Hello everyone! We are back with a series of Brain Teasers. In the next 3 weeks we will publish one teaser per week.
It will run until Friday and the winner will be announced on the next Monday, taking home the eBook version of Microsoft Windows PowerShell 3.0 First Look written by Adam Driscoll. We would like to thank our sponsor Packt, one of the most prolific and fast-growing tech book publishers in the world, for providing such a cool prize.

OK, time to start your engine, here goes the first one :)

You have a string, “PowerShell”, you need to break it to its individual characters so the result is a collection of System.Char objects:



1. You cannot cast the string to char array, e.g [char[]]”PowerShell”
2. You cannot use the String.ToCharArray method

The most shortest/elegant solution wins.

Good luck!

Filed in: Brainteasers Tags: ,

One Pingback/Trackback

  • http://twitter.com/JohnLudlowUK John Ludlow

    $s = “Powershell”; for ($i = 0; $i -lt $s.length; $i++) { $s[$i] }

  • http://twitter.com/p0w3rsh3ll _Emin_

    “PowerShell” -split”(?<=G.{1})"

  • http://HuddledMasses.org Joel “Jaykul” Bennett


    • dvsdee

      Sorry couldn’t resist: “PowerShell”[-1..-10]

  • http://twitter.com/mattifestation Matt


  • Crow


    • BacktoPoSh

      Correct me if I’m wrong, but this outputs String objects, not Char objects

      • Crow T

        That is right, the split op returns strings. To get an array of Chars
        from a String of any length you can do this:


  • Crow T


  • kevin

    For any string $test:


  • kevin

    Actually, you don’t need the parenthesis:


  • http://twitter.com/bielawb Bartek Bielawski

    ‘PowerShell’.GetEnumerator() ;)

    • Johnny Leuthard

      I think this one is best as it will take any string. Thetwo above it are great however if the string length changes it will break.

    • Crow T

      In your case GetEnumerator returns a CharEnumerator, not a Char[]

  • Mike F Robbins

    ([regex]::Matches(“PowerShell”,’.',’RightToLeft’) | % {$_}) -join ” + “`n”

  • Tobias Weltner

    “PowerShell” -split ‘(?<=.)(?=.)'

  • Tobias Weltner

    “PowerShell” -split ‘(?<=.)(?=.)'

    sorry if this is a repost. Somehow, my post vanished… :-)

  • Larry


  • Crow T


  • sahal

    “Powershell” -split “”

    • sahal

      Best one i see after reading others answers is:



    • Tobias Weltner

      Like this! Unfortunately, it returns two more (blank) characters…

  • http://www.facebook.com/vern.anderson.395 Vern Anderson


  • Peter

    “Powershell” -split “B” | foreach {[char[]]$arr += $_}

  • http://www.facebook.com/nat.davis.10 Nat Davis

    I love regex, how about this


  • Pingback: PowerShellでStringをChar[]に変換する « guitarrapc.wordpress.com

  • http://twitter.com/guitarrapc ギタパソ(PowerShellうぴゃー)


  • Chris Bennett

    “PowerShell”|%{for ($i=0;$i -lt $_.Length; ++$i) { $_[$i] }}

    or alternatively


    • Chris Bennett

      Wow, and after reading this is the same as doing


  • proftech

    Nice scripts… great contributors…

© 2014 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: