What is the difference between variables $a
and $b
?
$a = (Get-Date).DayOfWeek $b = Get-Date | Select-Object DayOfWeek
I tried to check
$a.GetType $b.GetType MemberType : Method OverloadDefinitions : {type GetType()} TypeNameOfValue : System.Management.Automation.PSMethod Value : type GetType() Name : GetType IsInstance : True MemberType : Method OverloadDefinitions : {type GetType()} TypeNameOfValue : System.Management.Automation.PSMethod Value : type GetType() Name : GetType IsInstance : True
But there seems to be no difference although the output of these variables looks different.
Use GetType to Get the Data Type of Variable in PowerShell You can use the GetType method to view its data type, as shown below. It displays IsPublic , IsSerial , Name , and BaseType properties of a variable. Output: The Name indicates the data type of a variable.
Compare-Object command in PowerShell is used to compare two objects. Objects can be a variable content, two files, strings, etc. This cmdlet uses few syntaxes to show the difference between objects which is called side indicators. => - Difference in destination object.
$_ is an alias for automatic variable $PSItem (introduced in PowerShell V3. 0; Usage information found here) which represents the current item from the pipe. PowerShell (v6. 0) online documentation for automatic variables is here.
First of all, you lack parentheses to call GetType. What you see is the MethodInfo describing the GetType method on [DayOfWeek]. To actually call GetType, you should do:
$a.GetType(); $b.GetType();
You should see that $a
is a [DayOfWeek], and $b
is a custom object generated by the Select-Object cmdlet to capture only the DayOfWeek property of a data object. Hence, it's an object with a DayOfWeek property only:
C:\> $b.DayOfWeek -eq $a True
Select-Object creates a new psobject and copies the properties you requested to it. You can verify this with GetType():
PS > $a.GetType().fullname System.DayOfWeek PS > $b.GetType().fullname System.Management.Automation.PSCustomObject
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With