You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The practices shown in example code are important, because inexperienced users will naturally assume that such code reflects best practices.
This is currently not always the case, as the following example demonstrates; in addition to this specific case, I think a systematic review of example code is worth doing.
The Compare-Object topic currently contains this hashtable definition:
Not only is the $(...) around the Get-Content commands unnecessary, using $(...) has the potential to modify the command's output.
Note: There is very specific Windows PowerShell bug where an if statement without an else block in a hashtable literal fails without $(...), but only if the if statement isn't the last entry.
To use a contrived example:
PS>@{ foo=$(,1) }.foo.GetType().Name
Int32 # Single-element array was unwrapped by $()
Similarly, (...), not $(...), should be used when output from a single command or expression is passed as a command argument (e.g., Write-Output (Get-Date), not Write-Output $(Get-Date).
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: a7615306-98e3-0b79-ac87-58bb191b4349
Version Independent ID: 85f1e969-8216-bed4-2204-409cbd9befd4
The practices shown in example code are important, because inexperienced users will naturally assume that such code reflects best practices.
This is currently not always the case, as the following example demonstrates; in addition to this specific case, I think a systematic review of example code is worth doing.
The
Compare-Object
topic currently contains this hashtable definition:Not only is the
$(...)
around theGet-Content
commands unnecessary, using$(...)
has the potential to modify the command's output.Note: There is very specific Windows PowerShell bug where an
if
statement without anelse
block in a hashtable literal fails without$(...)
, but only if theif
statement isn't the last entry.To use a contrived example:
Similarly,
(...)
, not$(...)
, should be used when output from a single command or expression is passed as a command argument (e.g.,Write-Output (Get-Date)
, notWrite-Output $(Get-Date)
.Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: