Looping in Powershell

As you already know, looping is one of the fundamental concepts of every programming language, and we use it for the things to be done repetitively.

In powershell, we have foreach loop which is used in scripting a lot. Take the following example for instance.

I create some variables and give each a value:

As you probably know, you can include an array of variables in an individual variable. for example, I can create a variable $Z and put these newly-created variables here.

If I get the value of $Z, the following is the output.

Now, I have a requirement of adding the letter “s” to the end of each variable. there are two ways to do so, one is to add it to the individual variables ($Q, $X, $Y), on the other hand we can use a foreach loop using a temporary variable.

which adds an “S” to the end of each variable. you can see the whole script as follows.

and the result:

Leave a Reply

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