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
$charArrayIn 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
dI executed the above PowerShell script, and you can see the output in the screenshot below:

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
$charArrayHere, the string "PowerShell" is split into individual characters, and the output will be:
P
o
w
e
r
S
h
e
l
lHere is the output in the screenshot below; you will also get the exact output:

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
$charArrayIn this example, the string "Scripting" is cast to a character array, resulting in the following output:
S
c
r
i
p
t
i
n
gRead 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
$charArrayHere, the string "Automation" is processed character by character, and the output will be:
A
u
t
o
m
a
t
i
o
nYou can see the output in the screenshot below:

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:
- How to Convert String to Boolean in PowerShell?
- Convert Strings to Lowercase or Uppercase in PowerShell
- How to Convert Variables to Strings in PowerShell?
- Convert a String to a Binary Number in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.