How to Convert a String to an Array of Characters in PowerShell?

Recently, I was working on a PowerShell script where I wanted to convert a string to an array of characters. I tried different methods. In this tutorial, I will show you how to convert a string to an array of characters in PowerShell with examples.

To convert a string to an array of characters in PowerShell, you can use the .ToCharArray() method, which directly converts the string into a character array. Alternatively, you can use the -split '' operator to split the string into individual characters or cast the string to a character array using [char[]]$string.

Convert a String to an Array of Characters in PowerShell

Now, let me show you different methods to convert a string to an array of characters in PowerShell with examples.

Method 1: Using the .ToCharArray() Method

The best way to convert a string into an array of characters in PowerShell is by using the .ToCharArray() method. This method is part of the .NET framework and can be directly applied to any string object.

Example

Here is an example and the complete PowerShell script.

# Define a string
$string = "Hello World"

# Convert the string to a character array
$charArray = $string.ToCharArray()

# Output the character array
$charArray

In this example, the string "Hello World" is converted into an array of characters, and the output will be:

H
e
l
l
o

W
o
r
l
d

I executed the above PowerShell script, and you can see the output in the screenshot below:

Convert a String to an Array of Characters in PowerShell

Check out How to Convert String to Int in PowerShell?

Method 2: Using the -split Operator

Another method to convert a String to an Array of Characters in PowerShell is by using the -split operator. Although the -split operator is typically used for splitting strings based on a delimiter, it can also be used to split a string into individual characters by providing an empty string as the delimiter.

Example

Here is an example.

# Define a string
$string = "PowerShell"

# Convert the string to a character array using -split
$charArray = $string -split ''

# Output the character array
$charArray

Here, the string "PowerShell" is split into individual characters, and the output will be:

P
o
w
e
r
S
h
e
l
l

Here is the output in the screenshot below; you will also get the exact output:

PowerShell Convert a String to an Array of Characters

Method 3: Using Array Casting

PowerShell allows you to cast a string directly to a character array using the [char[]] cast operator. Let me show you an example and the complete script.

Example

# Define a string
$string = "Scripting"

# Convert the string to a character array using array casting
$charArray = [char[]]$string

# Output the character array
$charArray

In this example, the string "Scripting" is cast to a character array, resulting in the following output:

S
c
r
i
p
t
i
n
g

Read How to Convert String to JSON in PowerShell?

Method 4: Using a Loop

You can use a foreach loop to iterate through each character in the string and add it to an array in PowerShell.

Example

Here is an example to do so.

# Define a string
$string = "Automation"

# Initialize an empty array
$charArray = @()

# Loop through each character in the string and add it to the array
foreach ($char in $string.ToCharArray()) {
    $charArray += $char
}

# Output the character array
$charArray

Here, the string "Automation" is processed character by character, and the output will be:

A
u
t
o
m
a
t
i
o
n

You can see the output in the screenshot below:

How to Convert a String to an Array of Characters in PowerShell

Conclusion

In this tutorial, I will show you how to convert a string to an array of characters in PowerShell using different methods like .ToCharArray() method, the -split operator, the concise array casting, the manual loop approach, etc.

I will recommend using the .ToCharArray() method to Convert a String to an Array of Characters in PowerShell.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.