EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Array of Strings

PowerShell Array of Strings

PowerShell Array of Strings

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

Powershell 1

When we check its datatype, it is the string type.

$str.GetType()

Powershell 2

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:

Powershell 3

Datatype of this variable.

$str.GetType()

Datatype

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

str length

When you declare the string array, its length is 0.

[String[]]$strarry
$strarry.Length

Or

$strarry = @()
$strarry.Length

Output:

str length 1

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:

index

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.

str 4

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.

output

When we check its length,

$hashstr.Length

Output:

hashtr

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.

hashtr count

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

String array

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:

get content

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 1

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 2

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:

example 3-1

To check if the string array is fixed size or not,

$str.IsFixedSize

Output:

PowerShell Array of Strings - Fixed Size

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:

PowerShell Array of Strings - ArrayList of the String

To remove the value from the arraylist,

$arrlist.Remove("Azure")
$arrlist

Output:

PowerShell Array of Strings - Remove Valuve

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:

PowerShell Array of Strings - Contains() method

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:

PowerShell Array of Strings - 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 –

  1. PowerShell Multiline String
  2. PowerShell Batch File
  3. Array in PowerShell
  4. PowerShell Dictionary
MICROSOFT POWER BI Training
48+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Certification Course
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Certification Course
26+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
WINDOWS POWERSHELL Certification Course
 26+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more