Definition of PowerShell Array of Strings
PowerShell array of strings is the collection of string objects that is multiple strings are residing into the same collection 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[]]
Using the empty array declaration
$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 the system.collections.arraylist class method as shown below.
New-Object -TypeName System.Collections.ArrayList
$arrlist = [System.Collections.ArrayList]@("PowerShell", "String", "Array")
$arrlist
How does array of string works in PowerShell?
When we just 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()
You can see the Type is converted to Object[] and the base type is converted to 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 gets increased, and actually, it 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:
Apparently, the Index which is not present will not get any output or will 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 a free text format and we can enter any number of the strings inside the Here-String method but let see if it 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 it counts the word means that here-string can’t be considered as a string array but as 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 we need to perform the 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
When You retrieve any file content using the Get-Content command, it is automatically converted to the 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: 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: Using the String ArrayList method to add 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 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: To check if the string array contains a specific string.
We need to 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 character won’t support this method.
$str.Contains("*Module*")
False
Example #5: Use the Select-String command to search from the string array.
When we deal with the files in the string array format and if we need to retrieve 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 life and we need to use it in the script to manipulate the items, retrieve the specific data from the string, replacing the data, and storing 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 works in PowerShell? examples with code implementation. You may also have a look at the following articles to learn more –
2 Online Courses | 1 Hands-on Project | 4+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses