- Creating a loop through an array Using the “ForEach” PowerShell command | Basic introduction | Part 1#2
- Creating a loop through an array Using the “ForEach” statement and variables | PowerShell environment| Part 2#2
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 should I learn about “arrays”?
In PowerShell environment, we use the concept of “Arrays” many times, whether we know it or not. In case that 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” as it appears in Wikipedia:
[source of information – Array data type]
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.
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.”
The next question that can appear is – what are these “objects” or who are these “members”?
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 Array can contain 100% of a specific type of object or, a “subset” of specific objects with a specific character or a specific property.
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 PowerShell command –
will get a list of all Exchange Online mailboxes that considered as “user mailboxes.”
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 does Array is created?
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.
- Using basic PowerShell command – for example, each PowerShell cmdlets that start with the prefix “Get,” create an Array.
- 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”.
- 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.
- 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 as a tool 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 actually asking from PowerShell to create for as an “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 PowerShell command is – storing the “Array data” in the computer RAM, and only then, copy the information to the PowerShell console.
Beside of 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 a 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.
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:
In this case, we “ask” from Exchange to provide us a list of all Exchange users (an array of Exchange Online users), but add additional Filter, that includes the condition – “fetch information” only if the user title is – Administrator.
The next article in the current article series
It is important for us to know your opinion on this article