Simple solution: Enclose static variable references like [ex1]::count in parentheses, whenever you want to pass them as an argument to another command like Write-Host:
Write-Host ([ex1]::count)
Why is that necessary when simple variables like $someVar don't require parentheses? For the same reason that Write-Host 2+2 doesn't print 4 but literally 2+2: PowerShells often counterintuitive argument parsing mode.
PowerShell switches from normal expression parsing mode to argument parsing mode, whenever it sees a call to a command (compare with a call to a .NET function, which doesn't change parsing mode). This mode follows its own rules, e. g. dollar sign followed by variable name causes PS to resolve the variable - as expected. Surprisingly there isn't a rule that covers static variable references, so PowerShell falls back to interpreting the argument as literal string.
Enclosing [ex1]::count in parentheses fixes the problem by forcing the parser to leave argument mode and parse an expression.
Full code sample with fix:
class ex1 { static [int]$count = 0 ex1() { [ex1]::count = [ex1]::count + 1 # Argument parsing mode -> wrap static variable reference in parentheses write-host ([ex1]::count) } } $ex1 = [ex1]::new() $ex2 = [ex1]::new() $ex3 = [ex1]::new() [ex1]::count # Expression parsing mode -> no parentheses needed!
Note: The added last line is an example for PowerShells implicit output feature. By just naming a variable on its own line, PowerShell writes its value to standard output. As there is no command invocation involved, PowerShell is still in expression parsing mode, so we don't need to bracketize the expression.
[ex1]::count = [ex1]::count + 1can be simplified to++[ex1]::count