r/PowerShell • • Sep 17 '20

PowerShell vs Python Reference

https://blog.ironmansoftware.com/powershell-vs-python/
206 Upvotes

51 comments sorted by

View all comments

5

u/azjunglist05 Sep 17 '20

Really awesome article! Just curious when getting a data type why:

$var = 1
$var | Get-Member

Instead of:

$var = 1
$var.GetType()

The latter, to me at least, more resembles the Python equivalent. Get-Member does a lot more than to just the get the type of the object. It also pulls the methods and properties available to that object.

1

u/purplemonkeymad Sep 18 '20

The pipe "unrolls" collections that are input (iterates over the elements). This means that Get-Member is working on the members in an array, where as .GetType() is working on the array. eg:

PS> $var = 1..4
PS> $var.GetType().tostring()
System.Object[]
PS> $var | Get-Member

   TypeName: System.Int32

You can use Get-Member to get all possible members of a mixed type array.

You don't want to choose the wrong one if you are testing for bad input or for multiple results.