简体   繁体   中英

Convert Get-WmiObject win32_diskdrive to array in Powershell 4

How can I convert a Get-WmiObject command to an array and add it to a combobox?

This is the command line:

Get-WmiObject -Query "select DeviceID from win32_diskdrive" | Select-Object -ExpandProperty  DeviceID | ft -HideTableHeaders -AutoSize | Out-String

And this is the output result:

\\.\\PHYSICALDRIVE0
\\.\\PHYSICALDRIVE1

I would like to write this like

"\\.\\PHYSICALDRIVE0","\\.\\PHYSICALDRIVE1"

Thanks for the help!

The output from Get-WmiObject ... |Select-Object ... is already an array - Format-Table -HideTableHeaders simply outputs each object line-by-line.

If you want the string you describe in your question, avoid the Format-* and the Out-String cmdlets altogether:

PS C:\> $DiskDrives = Get-WmiObject -Query "SELECT DeviceID FROM Win32_DiskDrive" |Select-Object -ExpandProperty DeviceID
PS C:\> $DiskDrives -join ','
\\.\PHYSICALDRIVE0,\\.\PHYSICALDRIVE1

For double-quotes ( " ) around each element, I would go for the -f format operator:

PS C:\> $DiskDrives.ForEach({'"{0}"' -f $_}) -join ','
"\\.\PHYSICALDRIVE0","\\.\PHYSICALDRIVE1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM