Casting to Void

It is possible to cast to System.Void to discard output. When using the StringBuilder example, this is a clean approach:

using namespace System.Text

$stringBuilder = [StringBuilder]::new()
[Void]$stringBuilder.AppendFormat('Name: {0}', $service.Name).AppendLine().
AppendFormat('Status: {0}', $service.Status).AppendLine().
AppendLine()
$stringBuilder.ToString()

However, when used with a command, it requires the use of extra parentheses, which can make it less appealing to use. This example uses Void to suppress the output from the Get-Command command:

[Void](Get-Command Get-Command)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.248.24