The array is a collection of items that belong to the same datatypes; the data type can be an “integer“, “float“, or “character“. However, the string only consists of the “character” datatype. So, one can say that a string is an array that supports only characters. The phenomenon of declaring a string as an array is known as an array of strings. For instance, when you have enormous data in string format, the better option is to store that string into an array. PowerShell array of strings allows manipulating data, such as if you want to retrieve any information you can do so using a variety of strings. In this guide, we will demonstrate the use of strings as an array:

How to Declare an Array of Strings Using PowerShell:

There are multiple ways to declare an array of strings. This section will list down and discuss all the possible ways to create an array of strings. So, let’s delve into it:

Method 1:

One possible method to create an array of strings is by using the method given below.

We have stored three strings in the variable named “string array“:

> $stringarray= @(“this is”, “Microsoft Windows”, “PowerShell terminal”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-583.png" data-lazy- height="253" src="data:image/svg xml,” width=”1349″>

Method2:

The other possibility to create an array string is using the class method System.Collections.Arraylist of PowerShell. The following commands will help you to create an array string:

> New-Object -TypeName System.Collections.Arraylist

> $stringarr=[System.Collections.Arraylist]@(“you are”, “working in”, “Windows PowerShell”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-584.png" data-lazy- height="341" src="data:image/svg xml,” width=”1351″>

How to Get String Array Variable Data Type:

In this section, we will take the $stringarray variable as an example. To get the data type of the variable, use the following cmdlet of PowerShell given below:

> $stringarray[email protected](“this is”, “Microsoft Windows”, “PowerShell terminal”)

> $stringarray.GetType()

When you run the GetType() cmdlet, it is observed that the datatype is stored in the “Name” column. A variable type is an object in the case of an array, and it will change to string in case of strings. Moreover, the value of the “BaseType” column is “System.Object” for strings, and it switches to “System.Array” in the case of arrays:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-585.png" data-lazy- height="320" src="data:image/svg xml,” width=”1349″>

How to Check the Length of the Array String:

As we have stored three strings in the $stringarray variable, one can check the length of the string using the command given below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-586.png" data-lazy- height="147" src="data:image/svg xml,” width=”851″>

How to Append Text to the Existing Array String:

Furthermore, you can add more values to the “array of string” variable. The values you want to append are operator ( =). Use the following command to append more importance to the variable “string array“:

> $stringarray = @(“and”, “ran with”, “administrative privileges”)

After appending three more strings, notice that the length of the “string array” variable has changed to 6:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-587.png" data-lazy- height="203" src="data:image/svg xml,” width=”815″>

How to Check Supported Methods of Existing Array String:

Once you have declared a string array and want to perform various operations, it is recommended to check which methods are supported by the string array. The following command will assist you in getting the supported methods:

> $stringarray | Get-Member -MemberType Method

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-588.png" data-lazy- height="594" src="data:image/svg xml,” width=”813″>

How to Find a String Inside a String Array?

In this section, we will guide you to find any string inside a string array. For instance, we have a string array variable that contains three strings:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-589.png" data-lazy- height="93" src="data:image/svg xml,” width=”579″>

If you want to find out whether it contains “PowerShell” or not, you have to get help from the “Contains” method, as shown below:

> $sa.Contains(“PowerShell”)

If $sa contains “PowerShell,“; then it will return “True“:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-590.png" data-lazy- height="73" src="data:image/svg xml,” width=”560″>

However, if you have checked for a wrong string, then it will return “False“. For example, we have searched for “ISE” as shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-591.png" data-lazy- height="73" src="data:image/svg xml,” width=”570″>

How to Change the Case of Letters in a String Array

One can change the letter’s case to upper or lower with the help of toUpper() and toLower() options. For example, we will create an array string $star to demonstrate this method:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-592.png" data-lazy- height="140" src="data:image/svg xml,” width=”676″>

Write the following command in PowerShell to change all the letters to uppercase:

You can check all the letters of the $star are converted to uppercase:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-593.png" data-lazy- height="133" src="data:image/svg xml,” width=”529″>

If you want to convert the letters to lowercase; you have to execute the following command:

All the letters are converted to lowercase:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-594.png" data-lazy- height="135" src="data:image/svg xml,” width=”518″>

Conclusion:

Arrays consist of the same variable types, while strings are basically arrays of character. If we dive into the phrase “Array of strings“, we can say that an array consists of an “array of characters“.

This article provides a detailed guide about an array of strings and how a string can be declared as an array. Moreover, we have described the different methods to declare string arrays and performed various operations on the string array.