12

I understand that semicolon is a command separator in Powershell. echo "hello"; dir gives this output.

PS C:\> echo "hello"; dir
hello

Directory: C:\

Mode         LastWriteTime     Length Name
----         -------------     ------ ----
d-----       2018-04-29 13:02         BCD_Backup
d-----       2018-12-02 14:08         Dell
<snip>

But why does date; dir give this output?

PS C:\> date; dir

Friday, December 14, 2018 11:14:23

PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\BCD_Backup
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName       : BCD_Backup
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : True
Name              : BCD_Backup
FullName          : C:\BCD_Backup
Parent            :
Exists            : True
Root              : C:\
Extension         :
CreationTime      : 2018-04-29 13:02:31
CreationTimeUtc   : 2018-04-29 11:02:31
LastAccessTime    : 2018-04-29 13:02:31
LastAccessTimeUtc : 2018-04-29 11:02:31
LastWriteTime     : 2018-04-29 13:02:31
LastWriteTimeUtc  : 2018-04-29 11:02:31
Attributes        : Directory
Mode              : d-----
BaseName          : BCD_Backup
Target            : {}
LinkType          :


PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\Dell
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\
<snip>
9
  • Interesting question. I hope it gets answered, because I have no idea myself, and I know my way around powershell.
    – LPChip
    Dec 14 '18 at 10:33
  • 1
  • 1
    I think this BUG is the result of PowerShells feature to postpone/defer output to not insert the same header for successive commands and seems to exist for quite a long time.
    – LotPings
    Dec 14 '18 at 11:55
  • @LotPings - what's fascinating is that Get-ChildItem doesn't appear to have any options that would generate the output that > date; Get-ChildItem results in. Dec 14 '18 at 13:23
  • Agreed, I'm also curious what is executed with date at all. It's not an alias nor a cmdlet or .exe file (same issue in PSv6 BTW)
    – LotPings
    Dec 14 '18 at 13:30
4

From Out-Default docs:

PowerShell automatically adds Out-Default to the end of every pipeline

Definitely, a semicolon itself does not suffice for recognizing such a state:

Get-Alias -Name gal; Get-Location
CommandType     Name                                          Version    Source
-----------     ----                                          -------    ------
Alias           gal -> Get-Alias

Drive        : D
Provider     : Microsoft.PowerShell.Core\FileSystem
ProviderPath : D:\PShell
Path         : D:\PShell

Adding Out-Default to the pipeline explicitly solves the problem:

Get-Alias -Name gal | Out-Default; Get-Location
CommandType     Name                                          Version    Source
-----------     ----                                          -------    ------
Alias           gal -> Get-Alias


Path
----
D:\PShell
3

As powershell executes statements one-by-one, I think, it applies output formatting of the first statement to all subsequent statements.

As Get-Date returns an object of DateTime type, it gets formatted as list, affecting your 'dir' output.

You can test this assumption by changing return type of Get-Date to string using 'format' option:

date -Format yyyy-MM-dd ; dir

(this will produce default output for 'dir')

Or by changing default output formatting by pipelining it to Format-Table:

 date | Format-Table ; dir
0

This seems to only occur when you use date; 'almost anything else after' that including other PS cmdlets (just tried a few).

'date' as typed appears to be calling core Windows (just like cmd.exe) to get that datestring back, PowerShell is trying to do something with that string it appears.

If you reverse this, 'anything here';date, does not exhibit this oddity. It just does this...

 dir;date


    Directory: D:\Scripts


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         9/1/2018   2:12 PM                .vscode
d-----         7/3/2018   4:44 PM                CheckURI

....


DisplayHint : DateTime
Date        : 12/14/2018 12:00:00 AM
Day         : 14
DayOfWeek   : Friday
DayOfYear   : 348
Hour        : 14
Kind        : Local
Millisecond : 516
Minute      : 42
Month       : 12
Second      : 22
Ticks       : 636803953425164049
TimeOfDay   : 14:42:22.5164049
Year        : 2018
DateTime    : Friday, December 14, 2018 2:42:22 PM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.