Creating a Loop through an array Using the “ForEach” statement and variables | PowerShell environment| Part 2#2 4.67/5 (3)

14 min read
The current article, is the continuation of the former article, in which we provide a basic introduction to the subject of the way that we write a PowerShell command, the loop through an array using the “ForEach” statement.

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

Using a PowerShell variable

what is Variable

If we want to simplify the explanation of the term “variable,” we can relate to the variable component” as a “logical container that holds “data.”

PowerShell variable -The purpose

Most of the time, the “data” stored in the variable, can be considered as an “Array data”.

The “variable,” can be considered as an “essential component,” that is used quite often, when using a PowerShell “ForEach statement” and arrays.

Using variable – The Flow of events

When using a PowerShell variable, the following phases or steps are implemented:

  • Step 1#3 – in the first phase, we declare the variable, and the “variable store” is empty.
  • Step 2#3 – In the second phase, we store information in the “variable store,” by “redirecting” the output of PowerShell “Get” commands.
  • Step 3#3 – Later, we use an additional PowerShell command\s, that “access” the information stored in the variable, and “do something” with this information.

The variable syntax in PowerShell environment.

The variable “syntax” is written in the following way:

  • Part 1#3 – We use the “$” sign for “declaring” a variable.
  • Part 2#3 – Then, we define the variable name. The variable name is an arbitrary name, whom we choose. The best practice is to choose a “meaningful” variable name. Variable names are not case-sensitive. Variable names can include spaces and special characters, but these are difficult to use and should be avoided.
  • Part 3#3 – Write the specific “PowerShell Get” command, that we use for “fetching data” about specific objects. The output from the PowerShell command, will be stored in the variable.

PowerShell variable -Naming convention-01

An example of variable syntax could be – $ExchangeMailboxes = Get-Mailbox

In this scenario, we defined a variable named- $ExchangeMailboxes ,that will use for storing the information that we get from the PowerShell command –
Get-Mailbox.

PowerShell variable -Naming convention-02

Using a PowerShell “ForEach” statement + Array

One of the most distinct advantages of implementing management tasks by using PowerShell, is the ability to perform a “Bulk operation” meaning, a process that will be executed on a “group” (Array) of objects.

The ability to run a specific task on array members is implemented many times by using a combination of the “ForEach” PowerShell statement + a variable that store the Array data (Array member’s).

We use the ForEach PowerShell statement for implementing the following flow:

  1. “Scanning” the Array content.
  2. Locate the first member in the array.
  3. Run a specific PowerShell command on the Array member (“do something”).
  4. Start the process all over again, by access the Array store, fetching the second member in the Array, run the requested PowerShell command and so on.

This loop process will stop after the “ForEach PowerShell statement’” locates the “last member” in the Array.

Running a PowerShell ForEach statement

In the following diagram, we can see an example of a PowerShell command
that includes the “ForEach PowerShell statement” + PowerShell variables.

  • Part 1#3 – in this part, we declare the name of the variable, and “charge” the “variable store” with the result of a “Get” PowerShell command.
  • Part 2#3 – in this part, we start the “ForEach PowerShell stamen” + declare the name of additional variable, that is used for representing a specific member in the Array.
  • Part 3#3 – in this part (that start with the curly brackets), we define the specific PowerShell command, that is “executed” separately for each member in the Array. This is the “Do Something” part. When using a Loop statement, the specific PowerShell command is written between curly brackets.

PowerShell ForEach statement syntax - The parts

How to address a member in the Array?

As we know, an Array is a collection of members.
When we use a PowerShell command that uses “ForEach PowerShell statement,” we wish to address or reference each of the members in the array, and “apply” a specific PowerShell command to each of the specific members.

Array and Array members

Technically speaking, there are a couple of methods that we can use to relate to the “Array member object.”

In the current article, I would like to review the method, in which we use an additional variable, that represents the “member object.”

The ForEach PowerShell statement, “knows” how access the “Array space” (represented by the “Array variable”), and loop through the Array.

In each Loop, the ForEach PowerShell statement “pull” a specific member from the Array (represented by the “Member variable”), run the required PowerShell command, and continue to the “next Array member.”

The name of the variable that represents the array member, is just an arbitrary name, whom we choose.

The best practice is to choose a meaningful variable name, which helps us to easily distinguish between the “member” and the “Array.”

In the following diagram, we can see the structure of the “ForEach PowerShell statement” command, in which we define the Array and the member in the Array, by using two different variables.

In our example, the variable $Mailbox represent each member in the array and the variable $ExchangeMailboxes represent the Array himself.

How to address a member in the Array

To be able to demonstrate the concept of Variable and Array, let’s use the following two examples:

Example 1 – view a specific array member

In the next example, we use a Variable named $ExchangeMailboxes, for storing the array that is created via the use of the PowerShell command- Get-mailbox.

The “complete PowerShell sentence” is-

$ExchangeMailboxes = Get-Mailbox

In this phase, we have a Variable that contains one or more member (Exchange Online mailboxes). Theoretically, we cannot “know” what is stored in the “Variable space.”

To be able to view the first Array member stored in the Variable $ExchangeMailboxes, we can use the following PowerShell command:

$ExchangeMailboxes[0]

To view the second Array member, stored in the Variable $ExchangeMailboxes

$ExchangeMailboxes[1]

And so on.

PowerShell console output example

Example 2 – Writing a PowerShell loop statement the display each array member

The following, example has no “real purpose” besides of the purpose of demonstrating how to Loop statement is executed for each of the Array members.

In this example, we “ask” from PowerShell to fetch each of the Array members that are stored in the Variable $ExchangeMailboxes and write on the PowerShell console screen a notification that uses the following syntax:
This is one of the array members – “(Exchange mailbox)” + <Array member name>

PowerShell command example

PowerShell console output example

How to specify the identity of a specific Array member?

In this section, I would like to relate to the subject of – How to specify the identity of a specific Array member?

As mentioned, we want that a specific PowerShell command will run over each member of the Array.

To enable PowerShell to perform this task, we need to use a method which will help PowerShell to “relate” to each of the members separately. In other words, we need to “tell” PowerShell, what is the “identity” of each member in the Array.,

To specify the identity of a member in an Array in PowerShell environment, we use the following syntax:

The variable name that represents the “member object” + the “dot character” + one of the object properties that represent object identity.

Sound confusing and difficult to understand?

The only solution that I know of is – reading the provided information and the examples over and over until you are able to see the “complete picture.”

How to specify the identity of a specific Array member -01

In the following diagram, we can see an example of the syntax that we use for relating to a specific member in the Array.

In our example, the variable that we use for representing Array member is – $Mailbox and the “object identity property” that we use is – “DisplayName.”

How to specify the identity of a specific Array member -02

Object identity” and “identity property”

This brings us to the next question – what is the meaning of “object identity,” and what is the meaning of “identity property”?

The answer is that each “PowerShell object,” has an “identity” in a similar way that each of us has an identity.

To make the issue even more complicated, each PowerShell object can have multiple identities like each of us can have multiple identities such as – private name, Nickname, Social security number and so on.

What is the meaning of Object identity property -03

In the following diagram, we can see an example of all the available identities of Exchange mailbox in the Exchange Online based environment.

In case that you get a “headache” from the variety of the multiple identities, it’s OK!

In reality, we don’t use all the types of these “identities” but instead, only a specific set of identities that are commonly used.

The multiple Object identities - Exchange Online mailbox -04

Array members and the two common types of relationships

In this section, I would like to relate to the subject which I describe as “Array members and the two common types of relationships.”

Technically speaking, we can define four types of relationships between objects:

  1. One to one
  2. One to many
  3. Many to one
  4. Many to many

To be able to define “one to one” relationship, we don’t need to use an Array and “ForEach” PowerShell statement.”

Regarding the relationships which I describe as -“Many to Many,” I prefer not to relate to this type of relationship in the current article because it could become a little complicated.

In many scenarios, the most common relationships that we deal with are, the relationships which can be described as – one to many or many to one.

Array members and the two common type of relationships

One to many relationships – example

The relationship which defined as “One to many,” relate to a scenario, in which we want to enable a specific object (one) to “impact” multiple objects (Array).

In other words, enable a specific object to “do something” for each member in the Array of objects.

Array members and two type of relationships -One to many relationships -02

To be able to demonstrate such relationship, let’s use the administrative task of assigning permissions.

For example, John is the Exchange Online administrator. We wish to assign John, Full Access permission on each of the existing Exchange Online mailboxes.

  • In our example, that “Array” include all the Exchange Online mailboxes
  • Each Exchange Online mailbox considers as a Member in the Array.
  • John represents the “One” in the current relationship.
  • Exchange Online mailboxes represent the “Many” in the current relationship.

Many to one relationship – example

The relationship which defined as “Many to one,” relate to a scenario, in which we want to enable multiple objects (Many) to “impact” a specific or an individual object (one).

In other words, enable a Group of objects (multiple objects) to “do something” on a specific object.

To be able to demonstrate such relationship, let’s use the administrative task of assigning permissions.

For example, we would like to assign Full Access permissions to all the users who “belong” to the IT group, on the “Help Desk mailbox” (one) that serves as a mailbox for Helpdesk tickets.

  • In our example, the “Array” include all the IT members.
  • Each IT user considers as a Member in the Array.
  • The IT members represent the “Many” in the current relationship.
  • The “Help Desk mailbox” represents the “One” in the current relationship.

Array members and two type of relationships -Many to One relationships -03

Using PowerShell Loop statement + variables + define the two-common type of relationships

In this section, I would like to “rap” couple of subjects that we have reviewed, up until now, so we will be able to understand better the “whole picture.”

Using a PowerShell ForEach statement example – One to Many relationship’s examples

In the current section, we demonstrate a relationship which described as “One to Many.”

Scenario description

In our scenario, John is the Exchange Administrator.
We want to assign John Full access permissions on all the existing Exchange Online mailboxes.

PowerShell ForEach statement example - One to many relationships - 01

To be able to fulfill this requirement, we create a PowerShell Loop command, that includes the following parts:

Part 1#3 – In this part, we define a variable named – $ExchangeMailboxes, that will “hold” the output from the PowerShell command – Get-Mailbox.

The specified PowerShell command will get the “list” of all existing Exchange Online mailboxes.

$ExchangeMailboxes = Get-Mailbox

Part 2#3 – In this part, we define an additional variable named- $Mailbox, that is used for representing each of the members in the “Exchange mailbox Array” (the information stored in the variable – $ExchangeMailboxes).

ForEach ($Mailbox in $ExchangeMailboxes)

PowerShell ForEach statement example - One to many relationships - 02

Part 3#3 – In this part, we define the PowerShell command that will assign the user “John” a Full Access permission for each of the Exchange Online mailboxes.

Add-MailboxPermission –identity $Varibale.<Identity> -user <Identity> -AccessRights FullAccess

Referencing each array member identity

Notice the syntax that we use for referencing each of the Array members (each of the Exchange Online mailbox) identity.

The syntax that we use is – $Mailbox.DisplayName

The identity of each member is “written,” by using the variable name that temporary hold each of the members in the array ($Mailbox) + a specific “identity” property.

In our example, we use the “DisplayName” as the identity property.

The “combination” of these two parts, enable us to “identify” each of the Array members individually in the Loop process that is executed by the “ForEach PowerShell statement.”

PowerShell ForEach statement example - One to many relationships - 03

sing the PowerShell ForEach statement example – Many to One relationship example

In the current section, we demonstrate a relationship which described as “Many to One.”

Scenario description

The IT uses a shared mailbox named – HelpDesk, that serves for storing help desk tickets that the organization user open.

We would like to enable each of the IT members, to have a Full Access permission to the shared mailbox named – HelpDesk.

PowerShell ForEach statement example - Many to One relationships -01

Part 1#3 – In this part, we define a variable named – $ITMembers, that will “hold” the output from the PowerShell command – Get-User | Where {$_.Title -eq “Administrator”}

The specified PowerShell command will get the “list” of all existing users, which their title contains the value – “Administrator”.

$ITMembers = Get-User | Where {$_.Title -eq “Administrator”}

Part 2#3 – In this part, we define an additional variable named- $User, that is used for representing each of the members in the “IT array” (the information stored in the variable – $ITMembers).

ForEach ($User in $ITMembers)

Part 3#3 – In this part, we define the PowerShell command that will assign each of the IT Members, Full Access permission on the “Helpdesk mailbox”.

Add-MailboxPermission –identity <Identity> -user $user.<Identity> -AccessRights FullAccess

PowerShell ForEach statement example - Many to One relationships -02

Referencing each array member identity

Notice the syntax that we use for referencing each of the Array members (each of the IT members) identity.

The syntax that we use is – $User.Alias

The identity of each member in the Array, is “written,” by using the variable name that temporary hold each of the members in the array ($User) + a specific “identity property”.

In our example, we use the “Alias” as the identity property.

The “combination” of these two parts, enable us to “identify” each of the Array members individually.

PowerShell ForEach statement example - Many to One relationships -03


Now it’s Your Turn!
It is important for us to know your opinion on this article

Summary
Creating a loop through an array Using the
Article Name
Creating a loop through an array Using the
Description
The current article is the continuation of the former article, in which we provide a basic introduction to the subject of the way that we write a PowerShell command, the loop through an array using the “ForEach” statement.
Author
Publisher Name
o365info.com
Publisher Logo
Print Friendly

Related Post

Please rate this

Eyal Doron on EmailEyal Doron on FacebookEyal Doron on GoogleEyal Doron on LinkedinEyal Doron on PinterestEyal Doron on RssEyal Doron on TwitterEyal Doron on WordpressEyal Doron on Youtube
Eyal Doron
Share your knowledge.
It’s a way to achieve immortality.
Dalai Lama

Leave a Reply

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