How do I create a custom type in PowerShell for my scripts to use?

后端 未结 8 1228
遥遥无期
遥遥无期 2020-12-22 18:29

I would like to be able to define and use a custom type in some of my PowerShell scripts. For example, let\'s pretend I had a need for an object that had the following struc

8条回答
  •  有刺的猬
    2020-12-22 19:01

    Steven Murawski's answer is great, however I like the shorter (or rather just the neater select-object instead of using add-member syntax):

    function New-Person() {
      param ($FirstName, $LastName, $Phone)
    
      $person = new-object PSObject | select-object First, Last, Phone
    
      $person.First = $FirstName
      $person.Last = $LastName
      $person.Phone = $Phone
    
      return $person
    }
    

提交回复
热议问题