title: Powershell. Create a class file to hold Custom Objects?
body: <p>I use Powershell's custom-object command to hold data points. Custom-object creates just one object and assigns a variable to it. Can Powershell go one step further and create new classes from which objects can be made?</p>
  
  <p>In the examples below, I store three pieces of data: a server name, a timestamp, and the minutes since an event occurred on the server.</p>
  
  <p>When I was learning Powershell, I put all this into a two-dimensional array:</p>
  
  <pre><code>$record = @("Server","Timestamp","Minutes")
  for ($j = 0; $j -lt 10; $j++){
      $record += @("Server1","$(get-date)",$j)
      sleep 60
      }
  $record | export-csv -path c:\record.csv -no type information
  </code></pre>
  
  <p>export-csv doesn't play well with arrays, so I started using a custom object:</p>
  
  <pre><code>$record = @()
  for ($j = 0; $j -lt 10; $j++){
      $r = New-Object -TypeName PSObject
      $r | Add-Member -MemberType NoteProperty -Name Server -Value ""
      $r | Add-Member -MemberType NoteProperty -Name Timesteamp -Value ""
      $r | Add-Member -MemberType NoteProperty -Name Minutes -Value ""
      $r.server = "Server1"
      $r.timestamp = "$(get-date)"
      $r.minutes = "$j"
      $record += $r
      sleep 60
      }
  $record | export-csv -path c:\record.csv -no type information
  </code></pre>
  
  <p>That's exports correctly, and dealing with object properties is easier than dealing with columns in a two-dimensional array.</p>
  
  <p>But if I want to create several custom objects that aren't in an array, I have to write the custom-object code over and over again.</p>
  
  <pre><code>$server1 = New-Object -TypeName PSObject
  $server1 | Add-Member -MemberType NoteProperty -Name Server -Value ""
  $server1 | Add-Member -MemberType NoteProperty -Name Timesteamp -Value ""
  $server2 = New-Object -TypeName PSObject
  $server2 | Add-Member -MemberType NoteProperty -Name Server -Value ""
  $server2 | Add-Member -MemberType NoteProperty -Name Timesteamp -Value ""
  #ad nauseum
  </code></pre>
  
  <hr>
  
  <p>What if Powershell could design custom <em>classes</em> in addition to custom <em>objects</em>? Like OO programming languages do? Something like:</p>
  
  <pre><code>class record {
      -MemberType NoteProperty -Name Server -Value ""
      -MemberType NoteProperty -Name Timestamp -Value ""
      -MemberType NoteProperty -Name Minutes -Value ""
      }
  $server1 = new-object -TypeName record
  $server2 = new-object -TypeName record
  $server3 = new-object -TypeName record
  </code></pre>
  
  <p>Is that possible in Powershell?</p>