Reputation: 4557
I have six .txt files in a directory. So, I create a variable thus:
$foo = gci -Name *.txt
$foo
is now an array of six strings. In my case I have
PS > $foo
Extensions.txt
find.txt
found_nots.txt
output.txt
proteins.txt
text_files.txt
PS > $foo.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS > $foo.Count
6
I'd like to measure that object, so I pass it to Measure-Object:
PS > $foo | Measure-Object
Count : 6
Average :
Sum :
Maximum :
Minimum :
Property :
That's what I was expecting. However, I might also pass $foo
like this:
PS> Measure-Object -InputObject $foo
Count : 1
Average :
Sum :
Maximum :
Minimum :
Property :
That's not what I was expecting. What's going on here?
Upvotes: 3
Views: 629
Reputation: 26023
PowerShell may be able to help you out here. Let's take a look at Get-Help Measure-Object -full
and check out that parameter.
-InputObject <psobject>
Specifies the objects to be measured. Enter a variable that contains
the objects, or type a command or expression that gets the objects.
Required? false
Position? named
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
Pipeline input is accepted by value, so that means that it counts each line of $foo
and tallies them. For what it is worth, all of the example usages on Technet and in the Get-Help reference use pipeline input for this parameter.
Upvotes: 0
Reputation: 201692
When you execute:
$foo | measure-object
PowerShell automatically unrolls collections/arrays and passes each element down the pipeline to the next stage.
When you execute:
measure-object -inputobject $foo
The cmdlet does not internally unroll the collection. This is often times helpful if you want to inspect the collection without having PowerShell do its automatic unrolling. BTW the same thing applies to Get-Member. If you want to see the members on the "collection" instead of each individual element do this:
get-member -inputobject $foo
One way to simulate this in the pipeline case is:
,$foo | Get-Member
This will wrap whatever foo is (collection in this case) in another collection with one element. When PowerShell automatically unrolls that to send elements down the pipeline, the only element is $foo
which gets sent down the pipeline.
Upvotes: 8