Using Classes (Initializing Properties – Part 2)

by Feb 7, 2017

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!