Powershell array is a type of data structure, created to store a series of items and each item has its own unique array index number which allows users to access specific addresses or index number data. We use arrays to store multiple values separated by a comma.
Example:
$var = 10, 15, 20, 25, 30, 31
$var = 1..5
In the above example we declared an array (Int value type) using the assignment operator =, we stored 5 values in the array and in the second array we stored numeric numbers 1 to 5.
In Powershell objects of an array is system.object, to know the array data type we use the get GetType() method, see the below examples.
PS C:\ $var = 10, 15, 20, 25, 30, 31
PS C:\ $var.gettype()
OutPut:
IsPublic IsSerial Name BaseType
——– ——– —- ——–
True True Object[] System.Array
Declaring the array using the sub-expression operator
The array declared using the operator @(…) is really important and one needs to understand, we will be dealing with the large code scripts with custom objects and functions. Let’s use our array operator to declare an array.
$Var3 = @()
The above example has no objects or zero objects stored in it, let’s store something inside of it.
$var3 = @(“Hello I’m Raja”, “I’m Learning Powershell”, It’s Day1 so far”, “More 14 days to go!”)
The above array has 4 values in its index, to check the size of the index we have a property .count, here is how to write it.
$var3 = @("Hello Raja", "I'm Learning Powershell", It's Day1 so far", "More 14 days to go!") $var3.count
Output:
How to access the array index values and stored information sequentially, Let’s take the above array again.
$var3 = @(“Hello Raja”, “I’m Learning Powershell”, It’s Day1 so far”, “More 14 days to go!”)
This array size is 4, which means there are 4 values stored in it, here is how we need to access.
Index Number | Data Inside an Array |
$var3[0] | Hello My Name is Raja |
$var3[1] | I’m Learning Powershell |
$var3[2] | It’s Day1 so far |
$var3[3] | More 14 Days to go! |
Let’s write a script to print the same on the screen!
$var3 = @("Hello I'm Raja", "I'm Learning Powershell", It's Day1 so far", "More 14 days to go!") Foreach ($var in $var3) { $var }
Output:
Hello My Name is Raja |
I’m Learning Powershell |
It’s Day1 so far |
More 14 Days to go! |
like we discussed in our day 6 post, there are important methods and properties of an array, let’s see how to get them on screen.
Get-Member
This is the topmost command or most useful command one must learn, this command gives you the members, properties, and methods of objects and commands.
Syntax
Get-Member [-InputObject <PSObject>] [[-Name] <String[]>]
#Examples Get-Process | Get-Member $raja = "I'm learning Powershell" $raja | get-member #The above example commands bring all the objects, properties and methods are available
Now, let’s use it for our array $var3
$var3 | get-member
output
Name | MemberType |
—- | ———- |
Clone | Method |
CompareTo | Method |
Contains | Method |
CopyTo | Method |
EndsWith | Method |
Equals | Method |
GetEnumerator | Method |
GetHashCode | Method |
GetType | Method |
GetTypeCode | Method |
IndexOf | Method |
IndexOfAny | Method |
Insert | Method |
Deleting an array
To delete an array or a file, or a folder there is a command available in powershell Remove-Item.
Remove-Item variable:$var3
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply