PowerTip: A Couple Quick PowerShell Array Tricks

Summary:  Learn some quick tricks for using Windows PowerShell arrays.

Hey, Scripting Guy! Question How can I split the following string in the $a variable?

$a = “atl-ws-01,atl-ws-02,atl-ws-03,atl-ws-04”

Hey, Scripting Guy! Answer Use the split method :

$b = $a.split(“,”)


Hey, Scripting Guy! Question How do I join an array, such as the one in the $a variable shown here?

 $a = “h”,”e”,”l”,”l”,”o”

Hey, Scripting Guy! Answer Use the join static method from the string c

Comments (7)

  1. K_Schulte says:

    @Matheus: I don't know exactly what Ed means, but ….

    You could use the [string] type (class) to see which static members if offers:

    [string] | get-member -static

    The "join" method is listed now.

    If you want to know more about that method type: [string]::Join

    And you can guess the meaning of the parameters now!

    If you use the signature:

    static string Join(string separator, Params string[] value)

    this should work:  [string]::Join("",$a)


    Here we used an empty seperator, but you can use any string you like!

    I hope it helps


  2. Ben Wilkinson says:

    @Matheus Kamphorst  here is one way . . using the join operator. (there is also the split operator).

    # Join

    $a = "h","e","l","l","o"

    $a -join "-"

    > h-e-l-l-o

    # Split

    $a = "atl-ws-01,atl-ws-02,atl-ws-03,atl-ws-04"

    $a -split ","

    > atl-ws-01

    > atl-ws-02

    > atl-ws-03

    > atl-ws-04

    # Info under help topic

    help about_operators

    Another real life example is:

    $ENV:PSModulePath -split ";"

    > C:UsersadministratorDocumentsWindowsPowerShellModules

    > C:windowssystem32WindowsPowerShellv1.0Modules

  3. i dont get the second part,

    How do I join an array, such as the one in the $a variable shown here?

    $a = "h","e","l","l","o"

    Use the join static method from the string c.

    where i put the join?

  4. Anonymous says:

    $c = "h","e","l","l","o"


  5. Ed Wilson says:

    @Matheus Kamphorst

    PS C:> $a = "h","e","l","l","o"

    PS C:> [string]::join("",$a)


    @Ben Wilkinson,  @K_Schulte Thanks 🙂

  6. Walid Toumi says:


    here is also:

    PS II>  $a = "h","e","l","l","o"

    PS II> $_OFS=$OFS ; $OFS=[string]::Empty

    PS II> "$a"


    PS II> # or

    PS II> -join $a


    PS II> # or

    PS II> [string]::Concat($a)


    PS II> $a = "atl-ws-01,atl-ws-02,atl-ws-03,atl-ws-04"

    PS II> $a -replace ',',"`r`n"





  7. Zach says:

    In about_join it shows the -join unary operator.

    $z = "a", "b", "c"

    -join $z