I will show you here how to create a character array in PowerShell using various methods.
What is a Character Array in PowerShell?
In PowerShell, an array is a data structure that can store a collection of items. A character array is simply an array where each item is a character. Arrays in PowerShell are inherently flexible, allowing you to store multiple types of data, including characters.
Let us check out different methods to create a PowerShell character array with examples.
Method 1: Using the char[] Type Accelerator
# Create a character array from a string
$charArray = [char[]]'HelloWorld'
$charArrayThis will create an array with each character of ‘HelloWorld’ as an individual element in the array.
After I executed the PowerShell script using VS code, the output came like in the screenshot below.

Method 2: Using the .ToCharArray() Method
Another common method to create a character array in PowerShell is by using the .ToCharArray() method of a string object. This method converts the string into an array of characters.
# Define a string
$string = 'PowerShell'
# Convert the string into a character array
$charArray = $string.ToCharArray()
$charArrayThis will produce a character array with each letter from ‘PowerShell’ as a separate element. You can see this in the screenshot below:

Method 3: Using the foreach Loop
You can also use a foreach loop to iterate through each character of a string and add it to an array in PowerShell.
# Define a string
$string = 'ArrayExample'
# Create an empty array
$charArray = @()
# Loop through each character in the string and add to the array
foreach ($char in $string) { $charArray += $char
}
$charArrayWhile this method works, it is less efficient than the previous methods because it has to resize the array with each new character added.
This is another way to create a character array in PowerShell.
Method 4: Using the Array Subscript Operator
You can create a character array by using the array subscript operator in PowerShell. This method is handy when creating an array with specific characters.
# Create a character array with specific characters
$charArray = 'a','b','c','d','e'This will create a character PowerShell array containing the characters ‘a’, ‘b’, ‘c’, ‘d’, and ‘e’.
Create a Character Array of Variable Length in PowerShell
# Using New-Object cmdlet
$length = 10
$charArray = New-Object char[] $length
# Using the ::new() method in PowerShell 5.0+
$length = 10
$charArray = [char[]]::new($length)Both of these examples create an array of 10 characters, each initialized to the null character in PowerShell.
Conclusion
Creating a character array in PowerShell is a simple task that can be achieved through various methods. Whether you’re casting a string to an array, using the .ToCharArray() method, iterating with a foreach loop, or specifying the array length with New-Object or ::new(), PowerShell provides the flexibility to work with character arrays in a way that suits your needs.
In this PowerShell tutorial, I have explained how to create a character array in PowerShell using different methods like:
- Using the char[] Type Accelerator
- Using the .ToCharArray() Method
- Using the foreach Loop
- Using the Array Subscript Operator
You may also like:



