Updated May 19, 2023
Definition of PowerShell Array of Strings
PowerShell array of strings is the collection of string objects that is multiple strings residing into the same group, which can be declared using String [], @(), or the ArrayList and can be used in various ways like functions, in the file content, as a variable and can perform the multiple operations on the string array like Split, Join, trim, ToUpper, ToLower, etc.
Syntax:
Few methods are used to declare the PowerShell string array.
Using the variable declaration as a datatype.
[String[]]
The empty array declaration is used.
$str = @()
Using the array list.
[System.Collections.Arraylist]@()
How to create an array of strings in PowerShell?
As shown in the above syntax, we can leverage those methods to create the Array of strings.
To create a string array, we can declare the variable name.
[String[]]$var = "PowerShell", "String", "Array"
Another way is to create using the @().
$strarry = @("PowerShell", "String", "Array")
One more way is by creating a system.collections.arraylist class method as shown below.
New-Object -TypeName System.Collections.ArrayList
$arrlist = [System.Collections.ArrayList]@("PowerShell", "String", "Array")
$arrlist
How does an array of string works in PowerShell?
When we declare the single String, it can be represented as below.
$str = "This is a PowerShell String"
$str
Output:
When we check its datatype, it is the string type.
$str.GetType()
You can check typeName is a String, and BaseType is a System. Object.
When we add the multiple strings
$str = "This is a PowerShell String", "This is a second string"
$str
Output:
Datatype of this variable.
$str.GetType()
The Type is converted to Object[], and the base type is converted to a System.Array. Object[] Array means it can accept multiple strings.
String Array Indexing
To check the length of the String we entered,
$str.Length
When you declare the string array, its length is 0.
[String[]]$strarry
$strarry.Length
Or
$strarry = @()
$strarry.Length
Output:
Once we add the items, their length increases, which actually works on the indexing part. Every time we add the items to the string array, its index increase by 1, and it starts from 0.
[String[]]$str = "First String", "Second String", "Third String"
Write-Output "0th Index: $($str[0])"
Write-Output "2nd Index: $($str[1])"
Output:
The Index that is not present will not get any output or throw an exception like index 4 is not present, so $Str[4] will not give any output.
Here-String method – Not Useful
The String can also be declared with the Hare-String method. This method is an accessible text format, and we can enter any number of the strings inside the Here-String method but let’s see if it is only a string or the string array.
$hashstr = @'
First String
Second String
Third String
'@
The above $hashstr gives the three strings as an output.
When we check its length,
$hashstr.Length
Output:
It doesn’t give the length 3, but counting the word means that here-string can’t be considered a string array but a string. See the proof below.
$hashstr.Count
Output:
The total count is 1 means it is considered a single string.
Methods supported by String array
When performing various operations on the string array, we always need to check which methods are supported for the operation.
Once we have created a string array, we can use the below commands to find the methods.
$str | Get-Member -MemberType Method
Get-Content method
The Get-Content command automatically converts the content of a retrieved file to a system array. For example, we have a text file name Test.txt stored at the C:\temp location.
We will check its datatype,
$file = Get-Content C:\Temp\test.txt
$file.GetType()
Output:
Examples
Let us discuss examples of PowerShell Array of Strings.
Example #1: Adding value to the array string.
We can add values to the string array using
$str = @("PowerShell", "Azure", "AZ Module")
We have the above string array, and to add the value to the string array,
$str += "DevOps"
$str += "PowerCLI"
$str
Output:
Example #2: A changing case of the string array using PowerShell.
You can change the string array to the Upper case or the lower case, and for that, you need to use ToUpper() or ToLower()
$str = @("PowerShell", "Azure", "AZ Module")
$str.toUpper()
$str.toLower()
Output:
Example #3: The String ArrayList method adds values to an array.
When we declare a simple string array, we can’t add or remove the values from the Array using Add() or Remove() because it was created with a fixed size. See the example below.
$str = @("PowerShell", "Azure", "AZ Module")
$str.Add("PowerCLI")
Output:
To check if the string array is fixed size or not,
$str.IsFixedSize
Output:
To solve the above problem, instead of creating a fixed-size array, we can use the ArrayList of the String.
New-Object -TypeName System.Collections.ArrayList
$arrlist = [System.Collections.Arraylist]@("PowerShell", "Azure")
$arrlist.Add("PowerCLI")
$arrlist.Add("DevOps")
Output:
To remove the value from the arraylist,
$arrlist.Remove("Azure")
$arrlist
Output:
Example #4: Check if the string array contains a specific string.
We must use the Contains() method to check if the string array contains any specific string.
$str = @("PowerShell", "Azure", "AZ Module", "DevOps", "AzCLI")
$str.Contains("Azure")
$str.Contains("Dev")
Output:
Wildcard characters won’t support this method.
$str.Contains("*Module*")
False
Example #5: Use the Select-String command to search from the string array.
When dealing with the files in the string array format and retrieving the particular line from that file or the matching lines, we can use the Select-String method, as shown below.
$file = Get-Content C:\Temp\test.txt
$file
$file | Select-String -Pattern "new"
$file | Select-String -Pattern "line"
Output:
Conclusion
PowerShell string array is useful daily in the automation engineer’s life. We need to use it in the script to manipulate the items, retrieve the specific data from the String, replace the data, and store the command output in the text format.
Recommended Articles
This is a guide to PowerShell Array of Strings. Here we discuss definition, syntax, and parameters; how does the Array of string work in PowerShell? Examples with code implementation. You may also have a look at the following articles to learn more –