Example:

I have the following output from a command that detects disks:

/dev/sda -d ata # /dev/sda, ATA device 

I need to get the first two values ​​- /dev/* and disk type (ata)
In cmd it looks like this:

 for /F "tokens=1,3" %%a in ('C:\"Program Files"\smartmontools\bin\smartctl.exe —scan') ^ do echo echo %%a 

How to do the same in powershell ?
Thank!

PS: I tried to split the line through the split , after pulling the values, it turned out, but there can be a huge number of disks, so this option is no longer necessary, I would like to receive the field numbers.

  • one
    C:\"Program Files"\smartmontools\bin\smartctl.exe —scan | % {,-split$_} | % {$_[0,2]-join' '} C:\"Program Files"\smartmontools\bin\smartctl.exe —scan | % {,-split$_} | % {$_[0,2]-join' '} - PetSerAl
  • Still split, I'll go read about the syntax, thank you so much, @ PetSerAl! - zubat
  • @zubat describe in more detail which lines you get, in what form, or by what command and which ones you want to have on output, I will help you. - Paulo Berezini

1 answer 1

 ('/dev/sda -d ata # /dev/sda, ATA device' -split ' ')[0,2] 

or

 $string = '/dev/sda -d ata # /dev/sda, ATA device' ($string -split ' ')[0,2]