PowerShell自定义对象

时间:2023-12-15 16:45:56

前面的性能高

使用[pscustomobject][ordered]强制类型转换

[pscustomobject][ordered]@{
Name= 'Boe'
Number = 1
ID = 007
}

使用[pscustomobject]强制类型转换

[pscustomobject]@{
Name= 'Boe'
Number = 1
ID = 007
}

初始化OrderedPSObject

New-Object pscustomobject -Property([ordered]@{
Name= 'Boe'
Number = 1
ID = 007
})

初始化PSObject

New-Object PSObject -Property @{
Name = 'Boe'
Number = 1
ID = 007
}

使用Select-Object

$Object = '' | Select-Object Name, Number, ID
$Object.Name = 'Boe'
$Object.Number = 1
$Object.ID = 007

借助Add-Member

$Object = New-Object PSObject
$Object | Add-Member -MemberType NoteProperty -Name Name -Value Boe -PassThru |
Add-Member -MemberType NoteProperty -Name Number -Value 1 -PassThru |
Add-Member -MemberType NoteProperty -Name ID -Value 007 -PassThru

From:http://www.pstips.net/performance-of-custom-psobject.html

http://www.pstips.net/powershell-convert-hashtable-to-object.html    hashtable转换为PS Object