18

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>

3 Answers3

9

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

JosefZ
  • 13,855
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
mikalai
  • 131
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
postanote
  • 5,136