PowerShell bug?

I did some searching, but I’m not sure if this is a bug. If I run these commands:

$now = Get-Date
$now | select *
$now

The $now var will have the contents of the select * output in it. If I output $now before running the select, it doesn’t do that like so:

$now = Get-Date
$now
$now | select *
$now

That does as expected and retains the value of $now. Am I missing something or is this a bug?

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  316

It works for me. If you do this you change the object type. So it will get formatted differently.

$now = get-date
$now = $now | select *

Are you doing this in the consolehost / ISE / VSCode?

Yet works for me in each case… (well, it deal not respond once, but restarting session resolved that.)

$now = Get-Date

$now

Friday, March 29, 2019 4:31:24 PM



$now | select *


DisplayHint : DateTime
DateTime    : Friday, March 29, 2019 4:31:24 PM
...




$now

Friday, March 29, 2019 4:31:24 PM



$MyDate = Get-Date

$MyDate

Friday, March 29, 2019 4:31:44 PM



$MyDate | select *


DisplayHint : DateTime
DateTime    : Friday, March 29, 2019 4:31:44 PM
...




$MyDate

Friday, March 29, 2019 4:31:44 PM


# # Using variable squeezing to assign and output the value

($now = Get-Date)

Friday, March 29, 2019 4:31:56 PM



($now | select *)


DisplayHint : DateTime
DateTime    : Friday, March 29, 2019 4:31:56 PM
...




($MyDate = Get-Date)

Friday, March 29, 2019 4:32:02 PM



($MyDate | select *)


DisplayHint : DateTime
DateTime    : Friday, March 29, 2019 4:32:02 PM
...

@dcskinner - This can never happen. Are you sure on the code you have posted ?

$now = Get-Date
$now
$now | select *
$now

kvprasoon, I was actually able to repro it, but as noted, restarting the session corrected it.

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

A discussion sort of about this that Bruce Payette comments on: “format-table should at least warn when it doesn’t display properties” https://github.com/PowerShell/PowerShell/issues/7871

@js - interesting. Good info and thanks for the link!