3

Hi every one I am new to power shell and trying to make sense about for each loop but I don't understand what does + $ means in the below script and why they are used together

$names = "jones","mike","Ash"
foreach ($name in $names)
{
    "$name = " + $name.length
}
1
  • I just copied this script from google but trying to make sense of for loop. What is $name in for each loop, i am confused because the variable we declared is $names Commented Jul 2, 2014 at 11:18

2 Answers 2

4

First of all, it is PowerShell. There is no space and observe the capitalization of letters.

Second, RTFM. There is a lot of documentation on Technet. Make use of that. There is built-in help. Read Help about_* topics.

Coming to your question, the foreach loop iterates over all collection. In your example, $names is an array of strings. An array is a collection.

In your example, when the foreach loop iterates over the collection, it copies each item in the collection to another variable called $name.

foreach ($name in $names) {}

Inside the foreach loop, you can use the $name variable to retrieve the value stored in the item. So, the following code will print the values in the collection.

foreach ($name in $names) {
    $name
}

$name is a string. So, $name.Length gives us the length of that string.

+ is an arithmetic operator. It can be used to concatenate strings. In this case, $name + $name.length will result in the value getting appended with the length.

Here is the modified example with output:

$names = "jones","mike","Ash"
foreach ($name in $names)
{
    $name + $name.Length
}

jones5
mike4
Ash3

Finally, coming to your example and the output:

$names = "jones","mike","Ash"
foreach ($name in $names)
{
    "$name = " + $name.Length
}

jones = 5
mike = 4
Ash = 3

I hope this provides some explanation for you on what the example is doing.

Sign up to request clarification or add additional context in comments.

Comments

2

Let's see this step by step:

$names = "jones","mike","Ash"

This declares an array with three string items and stores it in the $names variable.

foreach ($name in $names) { }

This is a loop that iterates through all items in the $names array variable. Each item is "copied" into the "$name" variable, so when you're working with $name, you are working with the current item in the loop.

"$name = " + $name.length

This outputs the contents of the $name variable and the "length" property of the object represented by $name. If you recall, we declared an array of string items, in other words string objects. Strings objects have properties and methods, one of those properties is the length property that tells how long the string is.

The notation is a bit odd, though. In PowerShell, variables embedded into string literals (e.g. "start $name end" will be replaced with their string value, so "start $name end" will become start jones end for the first item. However, you can't just write "start $name.length end", so in this example the author used + to concatenate (append) the two strings. An alternative solution to that would be "$name = $($name.length))": this way a single string literal would contain both the string and its length property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.