Skip to content

Commit 4fe1d90

Browse files
🩹 [Docs]: Add suppressing output guidance to PowerShell style guidelines (#225)
The PowerShell style guidelines now include comprehensive guidance on suppressing unwanted output from commands and methods. This addition helps developers choose the most performant approach when they need to discard function or method return values. - Fixes #224 ## Suppressing Output Section A new section has been added to the PowerShell style guidelines (`pwsh.instructions.md`) that provides clear guidance on: - **Using `$null =` for best performance** - The recommended approach for suppressing output from both cmdlets and .NET method calls - **Using `[void]` as an alternative** - Another valid option specifically for method calls that return values - **Avoiding `| Out-Null`** - Explicitly noting that this approach has significantly slower performance and should be avoided The section includes practical examples showing both correct and incorrect usage patterns, making it easy for developers to follow the best practices. ## Performance Impact This guidance is especially important in performance-critical code and loops, where the overhead of `| Out-Null` can accumulate and cause noticeable slowdowns. By using `$null =` or `[void]`, developers can ensure their scripts run efficiently.
1 parent 23e6f84 commit 4fe1d90

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎.github/instructions/pwsh.instructions.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,36 @@ function Get-ProcessedData {
515515
}
516516
```
517517

518+
## Suppressing Output
519+
520+
- Use `$null =` to suppress unwanted output from commands
521+
- Avoid using `| Out-Null` as it is significantly slower
522+
- Use `[void]` for method calls that return values you want to discard
523+
- This is especially important in loops or performance-critical code
524+
525+
**Good:**
526+
527+
```powershell
528+
# Suppress output from .NET method calls
529+
$null = $list.Add($item)
530+
$null = $collection.Remove($item)
531+
532+
# Alternative for methods
533+
[void]$list.Add($item)
534+
535+
# Suppress output from cmdlets
536+
$null = New-Item -Path $path -ItemType Directory
537+
```
538+
539+
**Bad:**
540+
541+
```powershell
542+
# Slower performance with Out-Null
543+
$list.Add($item) | Out-Null
544+
$collection.Remove($item) | Out-Null
545+
New-Item -Path $path -ItemType Directory | Out-Null
546+
```
547+
518548
## Pipeline
519549

520550
- Design functions to accept pipeline input when appropriate

0 commit comments

Comments
 (0)