How to Create an Array With PowerShell

In the Sunday blog post, I will show you something simple but very helpful. How to create an Array with PowerShell and use it.

Array

Before we dive into the how and cmdlets, let’s take a minute to understand what is an Array. An array is a variable type that stores a collection of items and provides a structured method to access the data inside it.

Create An Array

Let’s start with a simple example on how to create an array that stores numbers between 1 to 100.

$ = 1..100

To view the arrary I will type

$a

To access the value in the 10th spot in the array I will type

$a[10]

Note: The first spot in the aaray is alway 0 not 1.

Get Day Of the Week

Let me show you another example of working with arrays. This time I will get the day of the week using an Array.

I will start with saving the date and time into an array.

$x = get-date

Now, I will extract the day only from the array using the following cmdlet.

$x.DayOfWeek

Processing…
Success! You're on the list.

Posted

in

by