April 17, 2016 at 9:28 am #37905
Can someone explain like I'm five the difference between those 2 commands:
1- Get-ChildItem | Get-Member
2- Get-Member -inputObject Get-ChildItem
The results of the commands are not the same.
Command1 output TypeName: System.IO.DirectoryInfo properties and methods of Get-ChildItem objects.
Command2 output TypeName: System.String
Then I tried to get more information via help :
Get-Help Get-Member -parameter inputObject
Specifies the object whose members are retrieved.
Using the InputObject parameter is not the same as piping an object to Get-Member. The differences are as follows:
— When you pipe a collection of objects to Get-Member, Get-Member gets the members of the individual objects in the collection, such as the properties of each string in an array of strings.
— When you use InputObject to submit a collection of objects, Get-Member gets the members of the collection, such as the properties of the array in an array of strings.
This is Chinese for me ... I don't get it.
April 17, 2016 at 12:47 pm #37910
The different is the object that is being evaluated by the get-member cmdlet
When you run the the get-childitem cmdlet, it returns objects of type System.IO.DirectoryInfo. When you pipe get-childitem to get-member
get-childitem | get-member
get-childitem is outputing System.IO.DirectoryInfo object to the pipeline, then get-member is getting System.IO.DirectoryInfo objects from the pipeline as the inputobject. That is why you get the System.IO.DirectoryInfo type information as a result of the get-member cmdlet
When you use get-childitem as inputobject in the following manner.
get-member -inputobject get-childitem
Powershell does not execute get-childitem, it simply passes it in as a string so you get the member info for a System.String
The same thing happens if you use a variable:
$children = get-childitem $children | get-member
The result of the above is the same as doing get-childitem | get-member. The System.IO.DirectoryInfo object stored in the $children variable are output to the pipeline. Then get-member gets System.IO.DirectoryInfo objects as input
When you put the variable itself as the input object however:
$children = get-childitem get-member -InputObject $children
The result is the members of type System.Object. This is because get-member is looking at the $children variable itself, not what is stored in the variable.
April 19, 2016 at 6:43 am #37984
Excellent reply. TYVM. 😀
You must be logged in to reply to this topic.