How to declare a static member in a Powershell 5 class?

If I have a PS 5 class and I need a static member in it - how to declare it?

Class Foo { [string] $Bar = 'static member' } 

So I could refer to it without an instance like this

 Write-Host [Foo]::Bar 

Is it possible?

+5
source share
1 answer

PowerShell 5 added the static for this:

 static [string] $Bar = 'static member' 

Demo:

 PS > class Foo { >> static [string] $Bar = 'static member' >> } PS > [Foo]::Bar static member PS > 
+7
source

All Articles