Skip to content

Creating a loop through an array Using the “ForEach” PowerShell command | Basic introduction | Part 1#2

In the current article, I would like to provide a basic introduction to the concept of “Array” and the “ForEach statement”, in PowerShell based environment (especially in Office 365 and Exchange Online based environment).

Creating a loop through an array Using the “ForEach” PowerShell article series

Creating a loop through an array Using the “ForEach” PowerShell article series includes the following articles:

  1. Creating a loop through an array Using the “ForEach” PowerShell command | Basic introduction | Part 1#2 (this article)
  2. Creating a loop through an array Using the “ForEach” statement and variables | PowerShell environment| Part 2#2

PowerShell loop and array

The combination of – Arrays and the Loop process (which implemented by using the PowerShell “ForEach statement”), is a very powerful tool, that enables us to perform a bulk administrative task on multiple objects in “one click”.

The “problem” (or the challenge if we want to use a more politically correct word) is that most of the Exchange or Office 365 administrators are a little scary from the “PowerShell monster”, and especially more “complex scenario” in which we need to use a PowerShell command that includes more then one line.

The good news is that If you’re ready to invest a little time by reading the following two articles, you will find that the terms “Array”, Loop process and “ForEach statement” are not so scary.

After you will learn how to “enroll” this tools, you will get a very useful tool that could improve and optimize your day to day administrative tasks!

Generally speaking, to the concept of the “Array” are relevant to any programming or script Language but as mentioned, in the current article, the examples are related to Office 365 environment.

What to learn about “arrays”

In PowerShell environment, we use the concept of “Arrays” many times, whether we know it or not. In case we want to understand better many of the PowerShell command syntax examples, and to effectively utilize “PowerShell,” we should have a basic understating of the “arrays” concept.

What is the meaning of Array?

Let’s start with a formal definition of the term “Array”:

In computer science, an array type is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices (identifying keys) that can be computed at run time by the program. Such a collection is usually called an array variable, array value, or simply an array.

By analogy with the mathematical concepts of vector and matrix, array types with one and two indices are often called vector type and matrix type, respectively.

In simple words, the term “Array” define a group or a list of “objects” that we can “address” or relate to when using a PowerShell command.

What is the meaning of array

The Array considers as a collection of objects or other term -collections of members.

Theoretically, the Array can be empty or, contain zero members, but most of the time, we use the “Array” for grouping or “binding” one or more “objects.”

What is the meaning of array members

The next question that can appear is – what are these “objects” or who are these “members”?

Array contain what

The terms “objects” or “member” is a very general term that we use for describing different types of “entities” that we want to address.

In PowerShell based environment, there is a multiple of examples to the term “objects.”

For example, an object can be considered as Exchange mailbox, Office 365 users, operating system process or even a “row” in a file.

An example of array members

An Array can contain 100% of a specific type of object or, a “subset” of specific objects with a specific character or a specific property.

For example:

The PowerShell command Get-Mailbox, will get a list of all Exchange mailboxes.

In other words, we can say that the PowerShell command Get-Mailbox, creates an Array that contains all the objects that considered as “mailbox object”.

The below PowerShell command will get a list of all Exchange Online mailboxes that are considered “user mailboxes.”

Get-Mailbox | Where {$_.RecipientTypeDetails -eq "UserMailbox"}

In other words, in this case, the Array that created contain only a specific subset or a “group” of Exchange mailboxes, that have a specific property RecipientTypeDetails, and considered as User mailbox.

How Array is created

The “Array content” can be created by

Another interesting question that we can ask is – what are the methods that we can use for creating an “Array”?

There are various ways for “creating” an Array.

  1. Using basic PowerShell command – for example, each PowerShell cmdlets that start with the prefix Get, create an Array.
  2. Information that stored in a file in a specific format such as CSV file, can also be considered as an “Array”. When we need to access information that stored in a CSV file, each “row” in the CSV file, considered as “array members,” and each column in the CSV file will be considered as a “member property”.
  3. Using Group – although we don’t use to relate to a “Group” as an “Array,” Group is actually an array. A “group” serve as a logical container that contains Group members, and this is the exact definition of Array.
  4. Writing manually each member – an additional method for creating “Array” is simply implemented by manually writing each of the Array members. For example, when we assign permission to the specific user mailbox, we can assign the permissions for multiple users (Array). The creation of the “Array” is implemented by writing down, each of the users (members) separated by the comma character.

PowerShell command for creating arrays

In this section, I would like to relate to the PowerShell command prefix Get as one of our main methods for creating an “Array.”

Each time we run a PowerShell command with the prefix Get, we are asking PowerShell to create an “Array”.

PowerShell Get command serve as A utility for creating Array

By default, the “Array content” will be displayed by the PowerShell command on the PowerShell console, but it’s important to emphasize that the first “action” that is implemented by the PowerShell command is – storing the “Array data” in the computer RAM, and only then, copy the information to the PowerShell console.

Besides viewing the “Array data” in the PowerShell console, we can use the Array information that is stored in the RAM and choose what we want to “do” with the data.

For example, we can export the array information to a file, “pipe” to information to another PowerShell command and so on.

An example, creating an array by using the PowerShell Get-Mailbox command

Exchange infrastructure includes dozens, even hundreds of different objects type such as Exchange recipients, groups, connectors, mailboxes and so on.

When we use PowerShell cmdlets such as Get-Mailbox, we are asking from Exchange Online to provide us information about a very specific object type – “Exchange mailboxes.”

When we write, the PowerShell Get-Mailbox, and “hit” the Enter key, PowerShell is automatically creating an “array space”, and “fill in” the array store with an information about all the existing Exchange mailboxes.

Fetch specific objects from an Array -01

An additional example of creating an array by using PowerShell

Another example of an Array that we create by using a PowerShell command, can be an example in which we use the following PowerShell command that create a filtered search such as:

Get-User | Where {$_.Title -eq "Administrator"}

In this case, we “ask” Exchange to provide us a list of all Exchange users (an array of Exchange Online users), but add an additional Filter, that includes the condition to – “fetch information” only if the user title is – Administrator.

Fetch specific objects from an Array -02

In the next article, we will look into Creating a Loop through an array Using the “ForEach” statement and variables | PowerShell environment| Part 2#2.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *