If Powershell command separator is ; (semicolon), why does "date; dir" make dir output extra details?
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>
Solution 1:
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
Solution 2:
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