PowerTip: Create Array in PowerShell


Summary: Use Windows PowerShell to easily create an array.

Hey, Scripting Guy! Question Can I use Windows PowerShell to create an array of strings and avoid typing quotation marks
           around all the strings?

Hey, Scripting Guy! Answer Instead of creating an array, such as this: $array = “a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”, use a single string,
           and split it at the comma. It will be much less typing, and a whole lot faster. Here is an example:

$array = 'a,b,c,d,e,f,g,h'.Split(',')

Comments (4)

  1. PetSerAl says:

    It can be shorter:
    $array = write a b c d e f g h

  2. justpaul says:

    Or, if you’re not into the whole brevity thing:
    $array = @"
    a
    b
    c
    d
    e
    "@ -split("`r`n")

  3. Steve says:

    Thanks Just Paul I am so annoyed at having to paste everything into editpad and replacing line breaks with ","!!!!!

Skip to main content