When working with arrays of strings in PowerShell, there are several operations that you can perform such as creating, accessing, and manipulating array elements.
The following methods show how you can do some common tasks with an Array of strings in PowerShell.
Method 1: Creating an array of strings
#create an array of strings
$languages = @("PowerShell", "Js","Python", "Go","Java")
This example will create an array of strings.
Method 2: Accessing elements in the array
#create an array of strings
$languages = @("PowerShell", "Js","Python", "Go","Java")
# Access the first element in the array
$firstItem = $languages[0]
This example will output the first item in the array.
Method 3: Adding elements to the array
#create an array of strings
$languages = @("PowerShell", "Js","Python", "Go","Java")
# Add an element to the end of array
$languages += "C#"
Method 4: Removing elements from the array
#create an array of strings
$languages = @("PowerShell", "Js","Python", "Go","Java")
# Remove the first element from an array
$langArray = $languages[1..($languages.Length - 1)]
This example uses array indexing to remove an element from the array.
Method 5: Iterating through the array
#create an array of strings
$languages = @("PowerShell", "Js","Python", "Go","Java")
foreach( $lang in $languages) {
Write-Output $lang
}
The following examples show how you can use these methods.
Create an Array of Strings in PowerShell
To create an array of strings in PowerShell, you can use the syntax @().
#create an array of strings $languages = @("PowerShell", "Js","Python", "Go","Java") # print the array Write-Output $languages
Output:
PowerShell
Js
Python
Go
Java
In this script, we created an array variable using @() named $languages with the string elements “PowerShell”, “Js”, “Python”, “Go”, “Java”.
Finally, the Write-Output cmdlet outputs the array of strings to the console.
Accessing Elements in the Array of Strings in PowerShell
You can use various methods to access elements in the array of strings in PowerShell.
You can use the commonly used indexing method to access an element at the specified index.
#create an array of strings $languages = @("PowerShell", "Js","Python", "Go","Java") # Access the first element in the array $firstItem = $languages[0] Write-Output $firstItem
Output:
PowerShell
In this script, we used array indexing to access an element from the array. It returns the first item in the array $languages.
Finally, we output the first item in the array using Write-Output cmdlet in PowerShell.
Adding Elements to the Array of Strings in PowerShell
You can use += syntax to add an element to the array of strings in PowerShell.
#create an array of strings $languages = @("PowerShell", "Js","Python", "Go","Java") # Add an element to the end of array $languages += "C#" Write-Output $languages
Output:
PowerShell
Js
Python
Go
Java
C#
In this script, we added a new element C# to the array of string $languages in PowerShell using += syntax.
Finally, we print the array of strings using the Write-Output cmdlet in PowerShell.
Removing Elements from the Array of Strings in PowerShell
You can remove an element from the array of strings using the array indexing in PowerShell.
#create an array of strings $languages = @("PowerShell", "Js","Python", "Go","Java") # Remove the first element from an array $langArray = $languages[1..($languages.Length - 1)] Write-Output $langArray
Output:
Js
Python
Go
Java
In this script, we used the array indexing to remove the first element from the array. It slices the array and extracts the portion of elements.
Finally, we output the $langArray with the Write-Output cmdlet in PowerShell.
Iterating through the Array of Strings in PowerShell
You can use the loop to iterate through the array.
#create an array of strings $languages = @("PowerShell", "Js","Python", "Go","Java") foreach( $lang in $languages) { Write-Output $lang }
Output:
PowerShell
Js
Python
Go
Java
In this script, we used a foreach loop to iterate through the array of strings and print them to the console using the Write-Output cmdlet.
Conclusion
I hope the above article on the array of strings in PowerShell is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.