Enumerating objects
Often, we will want to perform an operation on the objects that we’re working with. Most of the time, there will be a cmdlet to do this, but sometimes, there won’t. For instance, say we want to output the filename and path of some items in a folder. There is no convenient property that will produce just the filename and path; there are properties such as pspath
, which will get us what we want and a bit extra, but nothing that gets exactly what we want. There is, however, a method on the objects that are produced by Get-ChildItem
that will: tostring()
. We can execute this method on each item by enumerating them, like so:
Get-ChildItem myfiles | Foreach-Object -MemberName tostring
This will produce exactly the output I want, as follows:
Figure 3.12 – Basic enumeration
This is a pretty simple example. Like Where-Object
, Foreach-Object
has basic and advanced syntax, and the advanced syntax looks very similar to...