PowerShell live documentation

Method stdio

Class methods in PowerShell are different from functions created with the function keyword. They behave more like functions in languages like C#, requiring a single return value and checking its type.

This means they don't subscribe to the pattern of normal PowerShell functions that stdout is the return value and stderr is the error details. Instead, return is required to return a value and errors are handled with exceptions.

Stdout

Stdout is never output to the console, no matter the return type of the method.

  1. class C {
  2.     [String] Func1() {
  3.         Write-Output 'Func1: writing string to stdout'
  4.         return 'Func1: returning a string'
  5.     }
  6.     [String] Func2() {
  7.         cmd /c 'echo Func2: writing string to stdout'
  8.         return 'Func2: returning a string'
  9.     }
  10.     [UInt32] Func3() {
  11.         Write-Output 'Func3: writing string to stdout'
  12.         return 3
  13.     }
  14.     [UInt32] Func4() {
  15.         cmd /c 'echo Func4: writing string to stdout'
  16.         return 4
  17.     }
  18.     [Void] Func5() {
  19.         Write-Output 'Func5: writing string to stdout'
  20.     }
  21.     [Void] Func6() {
  22.         cmd /c 'echo Func6: writing string to stdout'
  23.     }
  24. }
  25. $c = [C]::new()
  26. $c.Func1()
  27. $c.Func2()
  28. $c.Func3()
  29. $c.Func4()
  30. $c.Func5()
  31. $c.Func6()
Stdout
Func1: returning a string
Func2: returning a string
3
4

Stderr

See the stderr stream page.

Stdin

TODO