xlogI125’s blog

パソコン作業を効率化したい

PowerShell練習 Select-ObjectのPropertyパラメーター

メモ

  • 指定したプロパティ名が存在しない場合、プロパティの値に$nullが設定される

使い捨てスクリプト

# PowerShell 5.1, Windows 11 (2023年6月頃)

$a = [PSObject]::new()
Add-Member -InputObject $a -MemberType NoteProperty -Name propNameA -Value 123

$b = [PSObject]::new()
$b | Add-Member -NotePropertyMembers @{propNameB = 456}

$c = [PSCustomObject]@{propNameC = 789}

$x = @($a, $b, $c)

$output = $x | Select-Object -Property @(
  "propNameA", "propNameB", "propNameC"
)

$x[0] | Format-List | Out-String | Write-Verbose -Verbose
# 詳細:
# propNameA : 123

$output[0] | Format-List | Out-String | Write-Verbose -Verbose
# 詳細:
# propNameA : 123
# propNameB :
# propNameC :