指定Format-Table后Get-Volume的FriendlyName列丢失求助
Get-Volume指定Format-Table属性后FriendlyName列值丢失的问题解决
环境
Microsoft Windows [Version 10.0.19044.3086]
问题重现
执行以下PowerShell命令,指定Format-Table的输出属性:
PS C:\Users\saukrs> Get-Volume | Format-Table -Property DriveLetter, FriendlyName, FileSystemType, DriveType, OperationalStatus, SizeRemaining, Size
输出结果中FriendlyName列无有效值:
DriveLetter FriendlyName FileSystemType DriveType OperationalStatus SizeRemaining Size ----------- ------------ -------------- --------- ----------------- ------------- ---- E Unknown Fixed OK 244047036416 244047675392 C NTFS Fixed OK 145694720 255405891584 D NTFS Fixed OK 1708548096 1000186310656 NTFS Fixed OK 86540288 529526784
但直接执行Get-Volume | Format-Table时,FriendlyName列能正常显示值:
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatus SizeRemaining Size ----------- ------------ -------------- --------- ------------ ----------------- ------------- ---- E Unknown Fixed Healthy OK 227.29 GB 227.29 GB C pagrindinis NTFS Fixed Healthy OK 141.2 MB 237.87 GB D New Volume NTFS Fixed Healthy OK 1.59 GB 931.5 GB NTFS Fixed Healthy OK 82.53 MB 505 MB
原因分析
这是PowerShell格式化系统的特性导致的:
Get-Volume的默认格式化视图(不指定-Property参数时)使用了预定义规则,其中显示的FriendlyName是计算属性,实际读取的是卷对象的FileSystemLabel原生属性。- 当显式指定
-Property参数时,PowerShell会直接读取对象的原生FriendlyName属性,而这个属性在大多数卷对象上是空值,因此显示为空。
解决方法
方法1:复用默认格式化视图的规则
调用Get-Volume的默认格式化视图,同时指定需要的列,既保留自定义列,又能正确显示FriendlyName:
Get-Volume | Format-Table DriveLetter, FriendlyName, FileSystemType, DriveType, OperationalStatus, SizeRemaining, Size -View (Get-FormatData -TypeName Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Volume).FormatViewDefinition[0].Name
方法2:手动定义计算属性
显式创建计算属性,直接读取存储友好名称的FileSystemLabel属性:
Get-Volume | Format-Table DriveLetter, @{Name='FriendlyName'; Expression={$_.FileSystemLabel}}, FileSystemType, DriveType, OperationalStatus, SizeRemaining, Size
内容的提问来源于stack exchange,提问作者saulius2
相关产品推荐
相关产品推荐

