Forum Discussion
Get-ChildItem | Write-Host
The command in the title lists all the short names of files and folders in the current folder, whereas `Get-ChildItem * | Write-Host` lists the full names (including paths). I compared the output of `Get-ChildItem | gm` with the output of `Get-ChildItem * | gm`: no difference. If the very same objects are piped into Write-Host, how can the cmdlet give consistently different outputs?
3 Replies
Small addition: -Filter is also the most performant option, because the filtering is done by the provider before PowerShell creates any objects.
That’s why it’s generally preferable when possible.
Hi,
The difference you’re seeing doesn’t come from the objects themselves, but from how PowerShell resolves the pattern before sending data into the pipeline.
Here is a simple example that shows it clearly:
Get-ChildItem -Filter *.log | Write-Host # Filter applied after enumeration → Name Get-ChildItem -Path *.log | Write-Host # Wildcard resolved before GCI runs → FullName Get-ChildItem *.log | Write-Host # Same → FullName✔️ Why this happens
- With -Filter, PowerShell enumerates the directory first, then applies the filter.
The pipeline receives directory entries, and their default string representation is the Name. - With -Path or a direct wildcard (*.log), the wildcard is resolved before Get-ChildItem runs.
The pipeline receives resolved full paths, so Write-Host prints the FullName.
✔️ If you want to always display only the file name
Get-ChildItem *.log | Select-Object -ExpandProperty Nameor:
Get-ChildItem *.log | ForEach-Object { $_.Name }Hope this helps!
- BosjabouterCopper Contributor
Charlie34000, thank you for your elaborate answer.
As I understand you, you are saying that Get-ChildItem passes objects into the pipeline that have the properties Name or FullName depending on the presence of either the parameter -Filter or -Path. This makes sense to me.
But, I had hoped (and this was really what my question was about) that Get-Member would show the differences. I compared by computer the outputs of Get-ChildItem * | gm > a.txt and Get-ChildItem | gm > b.txt . The only difference between a.txt and b.txt is the value of PSChildName (a.txt and b.txt, respectively). That while Write-Host acts differently on the commands that generate a.txt and b.txt, just as it acts differently with -Filter or -Path in the parameter list.
Apparently, I expected too much of Get-Member and I have to revise accordingly my understanding of the PowerShell pipeline. My main purpose in this discussion is to find the logic behind the rules that cmdlets and objects follow in their interaction with pipelines and I had hoped that Get-Member would be a useful tool.
- With -Filter, PowerShell enumerates the directory first, then applies the filter.