Different object types exist in PowerShell, as they do in other programming languages. These types are considered as a technique of defining a form of behavior pattern for each object. Arrays, strings, and Hashtables are some of the more common object types. Each of these types serves as a storage location for items and has specific behavior.

Like we use arrays in other languages, PowerShell arrays also store one or more items. An integer, string, a generic object, or any other array can all be utilized as items. An array comprises all of these items. Data structures can be integrated and manipulated using arrays. In this article, you will show you how to use an array of strings in your PowerShell. So, let’s begin!

An Array of Strings in PowerShell

A simple array is created as a sequential block of memory in which each value is stored next to the other. In contrast, a PowerShell string array is a combination of objects having string type.

In this type of array, you can store multiple strings, and you can create it using “@()”, “String[]”, or the “ArrayList“. In PowerShell, these arrays are used in a variety of ways.

Create an Array of Strings Using the [String[]] Method in PowerShell

To work with an array of strings in PowerShell, firstly, we have to create them. By using the “[String[]]” method, we will create a “$var” array of strings. This “$var” array of strings will contain the values: “PowerShell”, “String”, and “Array”.

> [String[]]$var = “PowerShell”, “String”, “Array”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1-11.jpg" data-lazy- height="297" src="data:image/svg xml,” width=”907″>

Create an Array of Strings in PowerShell Using the @() Method

Another method to create an array of strings in PowerShell is the “@()” method. Define your array name, and store its sting values within the () brackets after the “@” symbol.

> $strarry = @(“PowerShell”, “String”, “Array”)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2-11.jpg" data-lazy- height="287" src="data:image/svg xml,” width=”926″>

Create an Array of Strings Using the System.Collections.ArrayList Class in PowerShell

You can also utilize the “System.Collections.ArrayList” class for creating a string array. Open your Windows PowerShell ISE and create a new PowerShell script file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3-11.jpg" data-lazy- height="643" src="data:image/svg xml,” width=”786″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/4-10.jpg" data-lazy- height="547" src="data:image/svg xml,” width=”815″>

First of all, we will create an object the class “System.Collections.ArrayList“. After that, we will declare the array of strings as “$arrlist”. This method also utilizes the “@()” for defining the string values of an array. Now, write out the following code in your script for creating an array of strings using “System.Collections.ArrayList”:

New-Object -TypeName System.Collections.ArrayList

$arrlist = [System.Collections.ArrayList]@(“PowerShell”, “String”, “Array”)

$arrlist

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/5-10.jpg" data-lazy- height="310" src="data:image/svg xml,” width=”861″>

Save this script as “testfile1.ps1”. After that, execute it using the “Run” button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/6-10.jpg" data-lazy- height="544" src="data:image/svg xml,” width=”920″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/7-11.jpg" data-lazy- height="342" src="data:image/svg xml,” width=”925″>

The execution of the script will list out the string elements of your array.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/8-8.jpg61181105e0fa1.jpg" data-lazy- height="197" src="data:image/svg xml,” width=”857″>

Create a Single-valued String Array in PowerShell

You can also declare an array comprising a single string in the following way:

$str = “This is a PowerShell String”

$str

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/9-9.jpg" data-lazy- height="263" src="data:image/svg xml,” width=”703″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/10-9.jpg" data-lazy- height="323" src="data:image/svg xml,” width=”845″>

Get Array Type in PowerShell

To get to know the type of your created array, invoke the “GetType()” method with your array variable.

$str = “This is a PowerShell String”

$str.GetType()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/11-8.jpg" data-lazy- height="264" src="data:image/svg xml,” width=”791″>

Here, you can check the type of your array.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/12-6.jpg" data-lazy- height="240" src="data:image/svg xml,” width=”802″>

Create a Multiple-valued String Array in PowerShell

You can also add multiple strings in your array by specifying the string values in “ ”, separated by commas “,”:

$str = “first String”, “second  string”

$str

$str.GetType()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/13-5.jpg611811072f0fd.jpg" data-lazy- height="294" src="data:image/svg xml,” width=”857″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/14-5.jpg" data-lazy- height="291" src="data:image/svg xml,” width=”894″>

Get String Array Length in PowerShell

If you want to know the length of your array, or the total number of array indexes, utilize the “.length” property with the name of your array. The below-given script will demonstrate this procedure to you:

$str = “first String”, “second  string”

$str

$str.Length

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/15-7.jpg" data-lazy- height="294" src="data:image/svg xml,” width=”832″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/16-4.jpg" data-lazy- height="278" src="data:image/svg xml,” width=”876″>

When we add string elements to the array, the length of the array increases, which helps with indexing. The index of the string array increases by one every time we add items to it, and it starts at zero.

[String[]]$str = “1ST”, “2ND”, “3RD”


Write-Output “0th Index: $($str[0])


Write-Output “2nd Index: $($str[1])

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/17-2.jpg611811085f310.jpg" data-lazy- height="292" src="data:image/svg xml,” width=”818″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/18-2.jpg611811089c57d.jpg" data-lazy- height="286" src="data:image/svg xml,” width=”868″>

Add Values to the String Array in PowerShell

The “ =” operator is used to add more values in your array of strings after you declare it in your script.

In the below-given script, we have already created an array named “$str” of type string and stored some values in it. Now, we will add the “DevOps”, “PowerCLI” strings in it by utilizing the “ =” operator. This operator will append the specified values in the “$str” array.

$str = @(“PowerShell”, “Azure”, “AZ Module”)

$str = “DevOps”

$str = “PowerCLI”

$str

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/19-1.jpg" data-lazy- height="266" src="data:image/svg xml,” width=”731″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/20-1.jpg" data-lazy- height="254" src="data:image/svg xml,” width=”807″>

If you have used the “System.Collections.ArrayList” class for the creation of string array, then you can use the “Add()” method for adding more values in your array:

New-Object -TypeName System.Collections.ArrayList

$arrlist = [System.Collections.Arraylist]@(“PowerShell”, “Azure”)

$arrlist.Add(“PowerCLI”)

$arrlist.Add(“DevOps”)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/21-1.jpg" data-lazy- height="250" src="data:image/svg xml,” width=”867″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/22.jpg61181109a1fab.jpg" data-lazy- height="271" src="data:image/svg xml,” width=”840″>

Change Case of an Array of String in PowerShell

The “toUpper()” and “toLower()” are two functions that are used to change the case of string arrays in Uppercase and Lowercase respectively.

$str = @(“PowerShell”, “Azure”, “AZ Module”)

$str.toUpper()

$str.toLower()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/23.jpg61181109def37.jpg" data-lazy- height="269" src="data:image/svg xml,” width=”832″>

Execute this “testfile1.ps1“, which will print out the string values of your array in Uppercase and Lowercase.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/24-1.jpg" data-lazy- height="253" src="data:image/svg xml,” width=”811″>

Remove a Value from String Array in PowerShell

Utilize the “Remove()” method for removing an element from your array. Pass the array element as a parameter in the “Remove ()” and execute your script.

New-Object -TypeName System.Collections.ArrayList

$arrlist = [System.Collections.Arraylist]@(“PowerShell”, “Azure”)

$arrlist.Remove(“Azure”)

$arrlist

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/25.jpg6118110a61d69.jpg" data-lazy- height="242" src="data:image/svg xml,” width=”800″>

We have removed the “Azure” element from the string array; that’s why the output is only showing the “PowerShell” as array value.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/26-2.jpg" data-lazy- height="239" src="data:image/svg xml,” width=”734″>

Check Elements of an Array of String in PowerShell

The “Contains()” method is utilized for checking if a particular string exists as an array element. To utilize this method, specify the string value as a parameter in the “Contains()” function.

$str = @(“C”, “c “, “JAVA”, “HTML”, “c “)

$str.Contains(“JAVA”)

$str.Contains(“CB”)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/27-1.jpg" data-lazy- height="291" src="data:image/svg xml,” width=”839″>

$str” array contains “JAVA” but not “CB” as elements. So the output will show “true” for the first statement and “false” for the second statement, which is calling the Contains() method.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/28-1.jpg" data-lazy- height="278" src="data:image/svg xml,” width=”881″>

Conclusion

While working with scripts, the PowerShell array of strings is quite useful as it helps in the re-usability of code and saves a lot of memory. We need to utilize it in a script for manipulating elements, extract specific data from the string, replace the data, and save the command to show output in text format.

In this article, we have practically answered your question: how to use an array of strings in PowerShell? If you want to work with an array of strings in your PowerShell script, feel free to try the given methods!

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/talha-150×150.jpg6118110bb4377.jpg" height="112" src="data:image/svg xml,” width=”112″>

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.