How To Create Character Array In PowerShell?

I will show you here how to create a character array in PowerShell using various methods.

To create a character array in PowerShell, you can cast a string to a char array type using the cast operator [char[]]. For example, $charArray = [char[]]'string' will split the string ‘string’ into an array of characters. If you need to create an array of a specific length, you can use the new() method, like $chars = [char[]]::new($Count) where $Count is the desired length of the array.

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

The simplest way to create a character array in PowerShell is by using the char[] type accelerator. This method involves explicitly casting a string to a character array.

# Create a character array from a string
$charArray = [char[]]'HelloWorld'
$charArray

This 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.

powershell character array

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()
$charArray

This will produce a character array with each letter from ‘PowerShell’ as a separate element. You can see this in the screenshot below:

Create Character Array In PowerShell

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
}
$charArray

While 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

For more advanced scenarios, such as when you need a character array of a specific length in PowerShell, you can use the New-Object cmdlet or the ::new() method (available in PowerShell 5.0 and later).

# 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.

If you need a dynamically-sized collection of characters, consider using an ArrayList or a List[char] instead, which allows you to add or remove elements on the fly.

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: