I thought I had tried it with the normal console, but I can’t reproduce it today there. I first noticed it in VSCode and I know it also happened in ISE, both of which I can reproduce it in today. Maybe this only happens when you run it as a script. Indeed, if I put it in a ps1 file and run it from the console, I can reproduce it as well.
After I posted this, I thought about it and realized it probably isn’t changing the contents of the variable, just how it decides to display the object. But I’m not sure why it would do that. Seems like a bug.
@js, I didn’t reassign the variable after line 1 like you did, I just outputted it. If I reassigned it, I would expect the object type to change.
@postanote, All of your tests appear to be my 2nd block of code. You assign, output, select, then output. Try assign, select, output to reproduce it (and note, as I explain above, I’m only getting this behavior in when running as a script).
@kvprasoon, I am sure. Take what you reposted and pop it in ISE and run it. Then remove line 2 and run it again and compare the output.
The key is that it’s a script. In a script, format-table and format-list run implicitly over the whole script, and they work in a funny way with multiple object types (more so format-table). Since select-object outputs a PSCustomObject, it will try to output any other type of object in the same way. There’s an obscure workaround where if you output an object type with a format file first, like a DateTime object ($now), things will format better. Get-childitem outputs both directory and file objects, but since they have format files, it works ok. All the original example was doing was outputting everything through format-list, instead of get-date normally running through format-custom.
# begin script
$now = Get-Date
$now | select *
$now
$(($now | select *).gettype()
$now.gettype()) | format-table
# end script
# begin output
DisplayHint : DateTime
DateTime : Monday, April 1, 2019 10:56:47 PM
Date : 4/1/19 12:00:00 AM
Day : 1
DayOfWeek : Monday
DayOfYear : 91
Hour : 22
Kind : Local
Millisecond : 690
Minute : 56
Month : 4
Second : 47
Ticks : 636897562076901760
TimeOfDay : 22:56:47.6901760
Year : 2019
DisplayHint : DateTime
Date : 4/1/19 12:00:00 AM
Day : 1
DayOfWeek : Monday
DayOfYear : 91
Hour : 22
Kind : Local
Millisecond : 690
Minute : 56
Month : 4
Second : 47
Ticks : 636897562076901760
TimeOfDay : 22:56:47.6901760
Year : 2019
DateTime : Monday, April 1, 2019 10:56:47 PM
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
True True DateTime System.ValueType
# end output