Powershell

How to Use an Array of Strings in PowerShell

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"

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")

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.

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

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

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

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

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

Here, you can check the type of your array.

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

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

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])"

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

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")

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

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

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

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

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")

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

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

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.