7

Running the cmdlets, the outputs are slightly different, and I'm just trying to understand the difference between the two cmdlets and why you would use one over the other.

Example:

Get-Date | Select -Property DayOfWeek

Output:

DayOfWeek
---------
Saturday


Get-Date | Select -ExpandProperty DayOfWeek

Saturday
Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
Laura
  • 105
  • 1
  • 4
  • Thank you for your help. I have tried several searches wording them several different ways trying to get any information on those commands, and I couldn't find anything. You just gave me a whole source of information for future questions I may have as well, so again, thank you! I am grateful :D – Laura Oct 21 '18 at 02:14

1 Answers1

5

Intro

You can inspect any object in Powershell by feeding it to Format-List cmdlet:

PS> Get-Date | Format-List

DisplayHint : DateTime
Date        : 2018-10-21 0:00:00
Day         : 21
DayOfWeek   : Sunday
DayOfYear   : 294
Hour        : 18
Kind        : Local
Millisecond : 28
Minute      : 38
Month       : 10
Second      : 36
Ticks       : 636757439160281486
TimeOfDay   : 18:38:36.0281486
Year        : 2018
DateTime    : 21 жовтня 2018 р. 18:38:36

Then, you can change the object, eg. create the new object with subset of properties of original object. You do this using Select-Object cmdlet and with the list of required properties in -Property parameter.

Select-Object has default alias Select, but I suggest that while learning Powershell and exchanging your code with external parties, eg. Superuser.com you do not use aliases, but only full names of cmdlets for the sake of clarity

Answer

  • Get-Date | Select-Object -Property DayOfWeek will create object which has only one property DayOfWeek of the object returned by Get-Date

  • Get-Date | Select-Object -ExpandProperty DayOfWeek will return the String with the content of DayOfWeek property

maoizm
  • 1,043
  • 10
  • 21
  • 1
    Those two parameters of `select` are explained more here too https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-6 – Vomit IT - Chunky Mess Style Oct 21 '18 at 16:16
  • 1
    @PimpJuiceIT thanks, this will be helpful as well. Looking at this question I have remembered that these "obvious" things like difference between situations when command returns string with something vs. returning simlarly-looking object are not clearly described in tutorials and I had to come through very non-intuitive learning curve until everything come to its places. – maoizm Oct 21 '18 at 16:25
  • Understood, sometimes the wording of explanations and such can be hard to interpret. Getting in there and running, comparing, and reading seems to work well for me but a more clear and concise explanation never hurts. – Vomit IT - Chunky Mess Style Oct 21 '18 at 16:55