当被引号包围时,Write-Host 打印整个对象而不是一个 属性

Write-Host prints entire object instead of one property of it when surrounded by quotes

$AllDrivesExceptBootDrive = Get-Volume | Where-Object {$_.DriveLetter -ne 'C'} | Select-Object DriveLetter, AllocationUnitSize

foreach ($d in $AllDrivesExceptBootDrive)
{
    Write-Host "Checking Drive $d.DriveLetter"
}

打印:

Checking Drive @{DriveLetter=F; AllocationUnitSize=4096}.DriveLetter
Checking Drive @{DriveLetter=G; AllocationUnitSize=4096}.DriveLetter
Checking Drive @{DriveLetter=E; AllocationUnitSize=4096}.DriveLetter
Checking Drive @{DriveLetter=H; AllocationUnitSize=4096}.DriveLetter
Checking Drive @{DriveLetter=I; AllocationUnitSize=4096}.DriveLetter

如何在 Write-Host 周围保留引号并仍然打印如下?

Checking Drive F
Checking Drive G
Checking Drive E
Checking Drive H
Checking Drive I

来自the about_Quoting_Rules help topic

Only simple variable references can be directly embedded in an expandable string. Variables references using array indexing or member access must be enclosed in a subexpression.

更改字符串文字以用子表达式运算符 $() 包围表达式,如下所示:

Write-Host "Checking Drive $($d.DriveLetter)"