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 nisargupadhyay87@outlook.com

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
How new debit, credit card rules will affect you? Details here
Cardless ATMs: How They Work, Top Pros And Cons | Bankrate
Places 5 Hours Away From Me
Weeminuche Smoke Signal
Workday Latech Edu
Mileage To Walmart
Autobell Car Wash Hickory Reviews
United Dual Complete Providers
Visustella Battle Core
Cvs Devoted Catalog
065106619
Condogames Xyz Discord
Letter F Logos - 178+ Best Letter F Logo Ideas. Free Letter F Logo Maker. | 99designs
Haunted Mansion Showtimes Near Millstone 14
Cocaine Bear Showtimes Near Regal Opry Mills
Aldi Bruce B Downs
Euro Style Scrub Caps
Violent Night Showtimes Near Century 14 Vallejo
Coomeet Premium Mod Apk For Pc
Watch Your Lie in April English Sub/Dub online Free on HiAnime.to
Gotcha Rva 2022
Ceramic tiles vs vitrified tiles: Which one should you choose? - Building And Interiors
Raw Manga 1000
Obituaries Milwaukee Journal Sentinel
Prep Spotlight Tv Mn
BJ 이름 찾는다 꼭 도와줘라 | 짤방 | 일베저장소
1979 Ford F350 For Sale Craigslist
Craigslist Comes Clean: No More 'Adult Services,' Ever
Issue Monday, September 23, 2024
Mia Malkova Bio, Net Worth, Age & More - Magzica
Craigs List Tallahassee
Basil Martusevich
Wega Kit Filtros Fiat Cronos Argo 1.8 E-torq + Aceite 5w30 5l
Murphy Funeral Home & Florist Inc. Obituaries
Craigslist Albany Ny Garage Sales
Keeper Of The Lost Cities Series - Shannon Messenger
Other Places to Get Your Steps - Walk Cabarrus
The best specialist spirits store | Spirituosengalerie Stuttgart
Weather In Allentown-Bethlehem-Easton Metropolitan Area 10 Days
3 Zodiac Signs Whose Wishes Come True After The Pisces Moon On September 16
boston furniture "patio" - craigslist
Thotsbook Com
Theater X Orange Heights Florida
Horseneck Beach State Reservation Water Temperature
Shiftselect Carolinas
The Plug Las Vegas Dispensary
Rubmaps H
Verilife Williamsport Reviews
Worlds Hardest Game Tyrone
ats: MODIFIED PETERBILT 389 [1.31.X] v update auf 1.48 Trucks Mod für American Truck Simulator
Affidea ExpressCare - Affidea Ireland
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.