Reputation: 4588
When I run get-childitem charsets.pack
, I see:
PS C:\opt\jdk\jdk7u67\jre\lib> Get-ChildItem .\charsets.pack
Directory: C:\opt\jdk\jdk7u67\jre\lib
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014-07-25 10:24 AM 2172877 charsets.pack
So when I run Get-ChildItem *.pack | ForEach-Object -Process {get-childitem $_.name}
, I expect to see the previous output repeated for every single file. Instead, I get
PS C:\opt\jdk\jdk7u67\jre\lib> Get-ChildItem *.pack | ForEach-Object -Process {get-childitem $_.name}
Directory: C:\opt\jdk\jdk7u67\jre\lib
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014-07-25 10:24 AM 2172877 charsets.pack
-a--- 2014-07-25 10:24 AM 1940370 deploy.pack
-a--- 2014-07-25 10:24 AM 221856 javaws.pack
-a--- 2014-07-25 10:24 AM 4163103 jfxrt.pack
-a--- 2014-07-25 10:24 AM 176166 jsse.pack
-a--- 2014-07-25 10:24 AM 532239 plugin.pack
-a--- 2014-08-11 11:30 AM 18345177 rt.pack
What gives? It looks like it's not actually running the process block for each object, and instead is doing some kind of optimisation. But when I run Get-ChildItem *.pack | ForEach-Object -Process {Get-Date}
, I get exactly what I expect:
PS C:\opt\jdk\jdk7u67\jre\lib> Get-ChildItem *.pack | ForEach-Object -Process {Get-Date}
Monday, 11 August, 2014 12:11:30
Monday, 11 August, 2014 12:11:30
Monday, 11 August, 2014 12:11:30
Monday, 11 August, 2014 12:11:30
Monday, 11 August, 2014 12:11:30
Monday, 11 August, 2014 12:11:30
Monday, 11 August, 2014 12:11:30
Fwiw, I'm far more used to bash, and am probably bringing in those expectations.
Upvotes: 1
Views: 873
Reputation: 36297
I think the real issue here is that you are misunderstanding how the pipe works. Your ForEach is outputting a [FileInfo] object to the pipe, at the end of the pipe since it has no output specified it outputs everything that reaches the end of the pipe to the formatter and that designates the collection as an array of [FileInfo] objects and displays them as shown in the OP.
If you want to see each file output you would have to pipe your Get-ChildItem to Out-String, or some such within the ForEach loop. Though that's really just doing double work since the original Get-ChildItem is passing the [FileInfo] object to the ForEach loop, and performing a Get-ChildItem $_.Name
(you probably meant FullName there) is providing the exact same [FileInfo] object, so that could be shortened to $_ | Out-String
.
So really you aren't outputting to the host during the ForEach loop, you are only feeding a FileInfo object into the ForEach loop to be processed, and then looking up that object and providing it down the pipe to be output late, exactly as if your ForEach loop were not there.
I hope that helps clear things up.
Upvotes: 4
Reputation: 47792
Get-ChildItem
is basically the equivalent of dir
and ls
(PowerShell actually aliases those to Get-ChildItem
).
So as it is now, you're trying to get children for individual files (and they have no children). The alternative would be to call Get-Item $_.Name
although that seems a bit silly considering that the output of that would be the same object that $_
refers to in the first place.
I may be misunderstanding what you want, but I think you just need to use $_
in your block.
Upvotes: 1