0

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!

Luca
  • 1,588
  • 2
  • 22
  • 26
Erick Batista
  • 21
  • 1
  • 3

1 Answers1

0

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"
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206