Nisarg Upadhyay
PowerShell script to save the output of array in variable

Using PowerShell to split a string into an array

February 5, 2020 by

In this article, I am going to explain the PowerShell script to split a string into an array. Before I explain it, let me just give you a small introduction of the PowerShell and the PowerShell array.

What is Microsoft PowerShell?

PowerShell is a command-line language provided by Microsoft. It is the replacement of the Windows batch scripting tool known as DOS. It is mainly designed for the IT administrators to automate and control the Windows operating systems and other applications.

Following are the benefits of the PowerShell:

  1. It has a simple way to manipulate the operating system components and configurations
  2. It is more secure than running VB and other application Scripts
  3. PowerShell allows complete access to the Microsoft.Net framework methods

Arrays in PowerShell

Arrays are the fixed-sized, sequentially ordered collection of the elements of any data types. It can be considered as the collection of the operators or variables. The arrays within the PowerShell can be processed using FOR loop and WHILE loop because all the elements in the array are of the same type, and the size of the array is known. The array in the PowerShell can be declared as follows:

Array Syntax:

Output:

Nisarg
Nirali
Dixit
Bharti

See the following output:

PowerShell Array

In this article, I am going to explain a few methods, as well as the PowerShell string functions that are used to convert the string into an array. I have used the following functions of PowerShell.

  1. .ToCharArray()
  2. .split()

The .TocharArray() function

The .TocharArray() function copies the characters of the string into the Unicode character array. .As I mentioned above, PowerShell can access all types of Microsoft.Net framework; hence we can use this function within the PowerShell script. Now, for example, we want to convert the string into the array of the word.

Convert Input String in Char Array:

Output

S
Q
L
S
e
r
v
e
r

Following is the output of the script

PowerShell script to split string into an array using .ToCharArray()

As you can see, we store the input string in $Inputstring variable, convert the string into the character array by using.TocharArray() function and store it in $CharArray variable and print each character within the array.

The .Split() function

The .Split() function splits the input string into the multiple substrings based on the delimiters, and it returns the array, and the array contains each element of the input string. By default, the function splits the string based on the whitespace characters like space, tabs, and line-breaks. If you want to split the string based on the specific character, then you must specify the character in the second argument. Following is the syntax of the .split() function.

For example, we want to split the following string into an array of multiple words by “-” character. Following is the code:

Convert the input string into the array of the words:

Output:

Microsoft
SQL
Server

Following is the output of the script:

PowerShell script to split string into an array using .split()

The above code splits the string based on the “-” character and saves the output in the $CharArray variable.

Example 1

If you want to access the specific word or the character in the output array, you must use the index of the array. Please note that the index of the array starts from 0. For example, in “Sonali Bhatt is Database Administrator” string, you want to print only “Sonali” and “Administrator” words. The index of the word “Sonali” within the array is zero, and the index of the word “Administrator” is four. The code should be written in the following way.

PowerShell Script:

Output:

Sonali
Administrator

Following is the output:

PowerShell script to save the output of array in variable

As you can see, we have assigned “Sonali Bhatt is a Database Administrator” string to $insputString variable, used .Split() function to split the string in multiple words and used index of the array to print the “Sonali” and “Administrator.”

This method can be used to split the string into two separate variables. For example, if you want to save the word “Database” in the $String1 variable and “Administrator” in the $String2 variable, then you should write the code in the following way.

PowerShell Script:

Output:

Sonali
Administrator

See the following image:

PowerShell script to save the output of array in variable

Example 2

If you want to convert the string into an array of the multiple substrings or words using the .split() function, then you must specify the additional argument named “number of the substring.” For example, we want to convert the string ”Nisarg, Nirali and Sonali are Database Administrators” in the array of the three substrings, and the entire string must be split with “,” delimiter then the code should be written in the following way:

PowerShell Script:

Output:

Nisarg Nirali Sonali are Database Administrators

Following is the output:

PowerShell script to split string into array: Array of Multiple substrings

Example 3

If you want to convert the string into an array of the multiple characters based on the arbitrary characters, then you must specify “\d” OR “\D”. If you specify “\d”, then it returns the array of the characters, and if you specify “\D” then, it returns the array of the numbers. For example, if we want to extract the “1987” from the “b1i9r8t7h” input string, then the code should be written as follows:

PowerShell Script:

Output:

1
9
8
7

Following is the output:

PowerShell script to split string into array: Array of numbers from arbitrary string

If you want to extract “birth” then the code should be written as following:

PowerShell Script:

Output:

b
i
r
t
h

Following is the output:

PowerShell script to split string into array: Array of characters from arbitrary string

Summary

In this article, I have explained the PowerShell scripting and array variable in PowerShell. Additionally, I also covered PowerShell scripts to split a string into an array using .toCharArray() and .split() functions.

Nisarg Upadhyay
PowerShell, String functions, T-SQL

About Nisarg Upadhyay

Nisarg Upadhyay is a SQL Server Database Administrator and Microsoft certified professional who has more than 8 years of experience with SQL Server administration and 2 years with Oracle 10g database administration. He has expertise in database design, performance tuning, backup and recovery, HA and DR setup, database migrations and upgrades. He has completed the B.Tech from Ganpat University. He can be reached on nisargupadhyay87@outlook.com

168 Views