EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Array of Strings
Secondary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

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 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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

[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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,294 ratings)

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:

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

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

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

index

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.

str 4

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.

output

When we check its length,

$hashstr.Length

Output:

hashtr

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.

hashtr count

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

String array

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:

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

example 3-1

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

$str.IsFixedSize

Output:

example 3

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:

example 4

To remove the value from the arraylist,

$arrlist.Remove("Azure")
$arrlist

Output:

example 4-1

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:

example 5

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:

select string

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 –

  1. PowerShell Multiline String
  2. PowerShell Batch File
  3. PowerShell do while
  4. PowerShell Dictionary
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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