Converting VBScript's Split Function

Definition: Returns a zero-based, one-dimensional array containing a specified number of substrings.

Split

“Returns a zero-based, one-dimensional array containing a specified number of substrings.” Granted, that doesn’t sound too terribly interesting. Nevertheless, the VBScript Split function (or the Windows PowerShell Split() method) can be incredibly useful. For example, suppose you have a comma-delimited list of computer names. As a single string value that’s of minimal use; as an array, however, that opens up a whole world of possibilities, including the opportunity to loop through each item in the array and perform a task against each of those items (i.e., each of those computers).

So how do you turn a delimited string into an array? Here’s how:

$a = "atl-ws-01,atl-ws-02,atl-ws-03,atl-ws-04"
$b = $a.split(",")

In the first command, we simply assign a series of computer names (separated by commas) to the variable $a. In the second command, we use the Split method to separate the list of names and store them in an array named $b. Note the sole parameter passed to the Split() method: ",", which simply indicates that the comma is used as the delimiter (or, if you prefer, separator).

When you run this command and then echo back the value of $b you should get the following:

atl-ws-01
atl-ws-02
atl-ws-03
atl-ws-04

Return to the VBScript to Windows PowerShell home page