GetType used in PowerShell, difference between variables

后端 未结 3 363
离开以前
离开以前 2021-01-30 19:22

What is the difference between variables $a and $b?

$a = (Get-Date).DayOfWeek
$b = Get-Date | Select-Object DayOfWeek

3条回答
  •  逝去的感伤
    2021-01-30 19:56

    Select-Object returns a custom PSObject with just the properties specified. Even with a single property, you don't get the ACTUAL variable; it is wrapped inside the PSObject.

    Instead, do:

    Get-Date | Select-Object -ExpandProperty DayOfWeek
    

    That will get you the same result as:

    (Get-Date).DayOfWeek
    

    The difference is that if Get-Date returns multiple objects, the pipeline way works better than the parenthetical way as (Get-ChildItem), for example, is an array of items. This has changed in PowerShell v3 and (Get-ChildItem).FullPath works as expected and returns an array of just the full paths.

提交回复
热议问题