i was trying to loop through a list of file names in $s4a using ForEach and ForEach-Object and display output. The only two ways i found that generate output are:
Can someone please let me know why the ones below generate nothing, while the above two work?
Thanks heaps
Essentially, it's because your syntax is incorrect for each of the statements that aren't working. In each of the non-working examples, you are not properly referencing the object that you wish the view the 'name' property on.
Select-Object is generally used when accepting an input object from the pipeline but can also identify the object using the -InputObject parameter.
When calling an object property by $object.propertyName, you don't need a select statement.
Instead of ForEach ($name in $s4a) {Select Name}
, try this: ForEach ($name in $s4a) {$name | Select Name}
ForEach ($name in $s4a) {$name | Select Name}
Instaed of ForEach ($name in $s4a) {Select $name.Name}
, try this: ForEach ($name in $s4a) {$name.Name}
Instead of $s4a| ForEach-Object {Select$_.Name}
$s4a| ForEach-Object {Select$_.Name}
, try this: $s4a| ForEach-Object {$_.Name}
$s4a| ForEach-Object {$_.Name}
Instead of $s4a| ForEach-Object {Select Name}
$s4a| ForEach-Object {Select Name}
, try this: $s4a| ForEach-Object {$_ | Select Name}
$s4a| ForEach-Object {$_ | Select Name}
Also, please note that there is a difference in how each method returns information. Select statements return modified versions of the original objects that only contain the properties that you are selecting. Whereas, calling a property directly returns only the value of that property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.