• Contact Us
  • About Us
  • Privacy Policy

TrustGuide

Guides and hows, tips for everyone

  • Home
  • Windows
  • Android
  • IT
    • Programming language
  • FaceBook
  • iPhone
  • Chrome
  • Photoshop

Back To Basics: How Foreach Loop In Powershell Works

October 4, 2022 by davidmr Leave a Comment

The most magical aspect of scripting is simple commands run over a sequence of instructions and produce results in a flash. For example, you can get it with the Foreach Loop In Powershell. 

This page shows how PowerShell’s Foreach loop works. So do the instructions to help your skills increase and enrich your insights.

How Foreach Loop In Powershell Works

Preparing A For Each Loop

To make a For Each loop, you must first have some data to iterate. This data set can be anything, though it lies within a single variable. You will need two variables: one representing the whole dataset and another depicting a specific record within the dataset.

To illustrate this point, imagine you have a Hyper-V Server housing several VMs, only a subset of which are now operational. To perform maintenance on the Hyper-V host server, you have to cease all running virtual machines via PowerShell.

The first order of business is to compile a list of all the active virtual machines. To accomplish this, use the following PowerShell cmdlet:

Get-VM | Where-Object {$_.State -eq ‘Running’}

The result of this command is depicted in Figure 1. For simplicity, note that you must do this command directly on the Hyper-V server. In a real task, you would probably use the -ComputerName parameter to redirect the command to a remote server.

Figure 1:

https://www.itprotoday.com/sites/itprotoday.com/files/ForEach%201.jpg

Via figure 1, TrustguideTeam shows you a list of local Hyper-V server virtual machines using the Get-VM cmdlet combined with the Where-Object cmdlet.

Without utilizing a For Each loop, it is still feasible to terminate all active virtual machines. To this end, you can add the pipe symbol and the Stop-VM cmdlet to the command we just listed. 

You will generally need to use a variable to keep track of the dataset you’re passing into the loop. To do this, I’ll make a new variable called $ListOfVMs and assign it to the command depicted in Figure 1.

Figure 2 shows that the list of running virtual machines will pop up after typing the variable’s name. Though all you see are VMs in this example, be mindful that a For Each loop may be adapted to work with any data type.

https://www.itprotoday.com/sites/itprotoday.com/files/ForEach%202.jpg

Figure 2: 

The $ListOfVMs variable (see Figure 2) is used to see the list of written virtual machines.

A variable representing a single data point is required after a variable representing the complete dataset ($ListOfVMs). Since this variable will represent each virtual machine in the list, we can call it $VM.

Set Up The For Each Loop

Everything required to initiate a For Each loop has been gathered.

The structure of the loop is as follows:

ForEach ($VM in $ListOfVMs){

Stop-VM $VM.Name

}

A glance at the code reveals that the loop is only three lines long.

The loop is declared on the very first line of code. PowerShell will carry out a set of operations on each virtual machine ($VM) in its current list of $ListOfVMs using this loop.

The being-done-something must be included in brackets. Towards single action (Stop-VM $VM.Name), you can employ various lines of code between brackets, notably when involved in complicated tasks. 

The shutdown command, Stop-VM, will operate while the virtual machine’s name supplied by $VM.Name iterates over in the loop. Figure 3 depicts the command’s format:

Figure 3:

https://www.itprotoday.com/sites/itprotoday.com/files/ForEach%203.jpg

Shortcut

However, the need to remove the $VM variable by using a shortcut. The $ListOfVMs variable and pipe it into the ForEach command. And here is how it looks like The full form of the command looks like this:

$ListOfVMs | ForEach Stop-VM $_.Name

As can be seen, this command functions identically to the other ones. Nonetheless, there is a significant distinction: You cannot apply  $VM.Name to indicate the VM’s name to be terminated if you remove the $VMs variable. Instead, just go for $_. Name for your virtual machine’s name. 

Look at figure 4 to know well:

Figure 4:

https://www.itprotoday.com/sites/itprotoday.com/files/ForEach%204.jpg

The Bottom Line

The Foreach loop in PowerShell works the same way in other languages. All that varies is how it works in particular tasks. Set your eyes on the variable, you employ during the process. As such, you can monitor your commands and avoid minor errors better.

By the end of this essay, you should have a firm grasp of the operation of this programming and other examples to refer to. Hopefully, this post is helpful for all of you guys. Contact TrustguideTeam on the spot if you get into any trouble.

Share this one with others as well.

Filed Under: Guideline

Leave a Reply Cancel reply

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

Recent Posts

  • Office 365 Active Directory Sync: Instructions Revealed!
  • How Does Powershell Delete Ad User? The Truth Revealed! 
  • How To Disable MFA Office 365? Easy Tips and Tricks! 
  • How To Setup Ldapsearch Windows Using AD LDS- Solved!
  • Exchange 550 5 7 1 Resolver RST Authrequired Authentication Required- Solved!

Copyright © 2023 · TrustGuide.net · Log in