Using Classes (Initializing Properties - Part 2)

Class properties can be assigned a mandatory type and a default value. When you instantiate an object from a class, the properties are pre-populated and accept only the data type specified:

#requires -Version 5.0
class Info 
  # strongly typed properties with default values
  $Name = $env:USERNAME

  $Computer = $env:COMPUTERNAME
  $Date = (Get-Date)

# create instance
$infoObj = [Info]::new()

# view default (initial) values

# change value
$infoObj.Name = 'test'

Twitter This Tip! ReTweet this Tip!