Using PowerShell to split a string into an array (2024)

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:

1

2

$Array=Nisarg, Nirali,Dixit,Bharti

$Array

Output:

Nisarg
Nirali
Dixit
Bharti

See the following output:

Using PowerShell to split a string into an array (1)

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:

1

2

3

$Inputstring ="SQL Server"

$CharArray =$InputString.ToCharArray()

$CharArray

Output

S
Q
L
S
e
r
v
e
r

Following is the output of the script

Using PowerShell to split a string into an array (2)

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:

1

2

3

$Inputstring ="Microsoft-SQL-Server"

$CharArray =$InputString.Split("-")

$CharArray

Output:

Microsoft
SQL
Server

Following is the output of the script:

Using PowerShell to split a string into an array (3)

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:

1

2

3

4

$Inputstring ="Sonali Bhatt is Database Administrator"

$CharArray =$InputString.Split(" ")

$CharArray[0]

$CharArray[4]

Output:

Sonali
Administrator

Following is the output:

Using PowerShell to split a string into an array (4)

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:

1

2

3

4

5

6

$Inputstring ="Sonali Bhatt is Database Administrator"

$CharArray =$InputString.Split(" ")

$String1= $CharArray[0]

$String2= $CharArray[4]

write-host $String1

write-host $String2

Output:

Sonali
Administrator

See the following image:

Using PowerShell to split a string into an array (5)

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:

1

2

3

4

5

$string = "Nisarg,Nirali,Sonali are Database Administrators"

$array= $string.Split(",")

$array[0]

$array[1]

$array[2]

Output:

NisargNiraliSonali are Database Administrators

Following is the output:

Using PowerShell to split a string into an array (6)

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:

1

2

3

$string = "b1i9r8t7h"

$array= $string -split "\D"

$array

Output:

1
9
8
7

Following is the output:

Using PowerShell to split a string into an array (7)

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

PowerShell Script:

1

2

3

$string = "b1i9r8t7h"

$array= $string -split "\d"

$array

Output:

b
i
r
t
h

Following is the output:

Using PowerShell to split a string into an array (8)

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.

  • Author
  • Recent Posts

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 [email protected]

Latest posts by Nisarg Upadhyay (see all)

  • Different ways to identify and change compatibility levels in SQL Server - July 22, 2024
  • Copy SQL Databases between Windows 10 and CentOS using SQL Server data tools - October 19, 2022
  • Changing the location of FILESTREAM data files in SQL Database - October 14, 2022

Related posts:

  1. SQL string functions for Data Munging (Wrangling)
  2. SQL Convert Function
  3. SQL TRIM function
  4. Top SQL Server Books
  5. Python scripts to split and concatenate strings
Using PowerShell to split a string into an array (2024)
Top Articles
Who Owns The Most Polkadot? | Polkadot Holders
Jak założyć portfel kryptowalut i Bitcoina?
Whas Golf Card
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Pwc Transparency Report
今月のSpotify Japanese Hip Hopベスト作品 -2024/08-|K.EG
Troy Athens Cheer Weebly
Fredericksburg Free Lance Star Obituaries
Fear And Hunger 2 Irrational Obelisk
Arre St Wv Srj
Overton Funeral Home Waterloo Iowa
Cyndaquil Gen 4 Learnset
Wicked Local Plymouth Police Log 2022
Dallas Craigslist Org Dallas
Selfservice Bright Lending
Bella Bodhi [Model] - Bio, Height, Body Stats, Family, Career and Net Worth 
Sullivan County Image Mate
Ups Drop Off Newton Ks
Joan M. Wallace - Baker Swan Funeral Home
Best Transmission Service Margate
2013 Ford Fusion Serpentine Belt Diagram
Johnnie Walker Double Black Costco
Hctc Speed Test
480-467-2273
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Craigslist Pasco Kennewick Richland Washington
Kqelwaob
Airg Com Chat
Revelry Room Seattle
Lininii
How Much Is An Alignment At Costco
Petsmart Distribution Center Jobs
Tamilrockers Movies 2023 Download
Craigslist Neworleans
Umiami Sorority Rankings
Wsbtv Fish And Game Report
Page 5662 – Christianity Today
Stanford Medicine scientists pinpoint COVID-19 virus’s entry and exit ports inside our noses
Section 212 at MetLife Stadium
Easy Pigs in a Blanket Recipe - Emmandi's Kitchen
Sams Gas Price Sanford Fl
Parent Portal Pat Med
Sechrest Davis Funeral Home High Point Nc
Spurs Basketball Reference
Conan Exiles Colored Crystal
Freightliner Cascadia Clutch Replacement Cost
Smoke From Street Outlaws Net Worth
What Does the Death Card Mean in Tarot?
Congressional hopeful Aisha Mills sees district as an economical model
Craigslist Monterrey Ca
Elizabethtown Mesothelioma Legal Question
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6125

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.