Powershell

PowerShell Array of Strings

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

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

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=@("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:

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:

> $stringarray.length

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:

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

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:

> $sa=@("Microsoft", "Windows", "PowerShell")

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“:

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

> $sa.Contains("ISE")

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:

> $star=@("PowerShell", "Command Line", "Terminal")

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

> $star=$star.toUpper()

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

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

> $star=$star.toLower()

All the letters are converted to lowercase:

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.

About the author

Adnan Shabbir