none
select -last doesn´t work RRS feed

  • Question

  • Hi there

    i want to figure out - for further scripting - the index of the smallest hard drive.

    So i used this one:

    PS C:\Windows\system32> gwmi win32_diskdrive -Filter {mediatype='fixed hard disk media'} | select index | sort -desc size | ft -property index -hidetableheaders | select -Last 1

    but i get an error..

    When i´m selecting the first line(s) its work - i mean i get an result:

    gwmi win32_diskdrive -Filter {mediatype='fixed hard disk media'} | select index | sort -desc size | ft -property index -hidetableheaders | select -First 4
    
    0
    1

    Now i have some questions:

    why is -first working and -last istn´t?

    the query is quite long - can i shorten it?

    i need the determined index number as a parameter for another tool - whats the best practice for this?

    should i write it in an variable? or in a temporary file? whats the easiest way?

    thx

    Thursday, May 21, 2015 11:10 AM

Answers

  • That is easy if you start correctly

    This gets the smallest disk:

    gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |
        sort size|
        select -first 1

    This gets the index of the smallest one.

    (gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |
         sort size|
         select -first 3).Index


    \_(ツ)_/

    • Marked as answer by logg.sar Thursday, May 21, 2015 11:55 AM
    Thursday, May 21, 2015 11:50 AM

All replies

  • This:

    select index | sort -desc size

    Selects only one property called "index" and then tries to sort by a property called "size" .  Size no longer exisits after the select.

    To get disks and select one by size is easy.

    gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |sort size|select -first 1

    You need to spend some time reading the help to learn the correct syntax and how the CmdLets are designed to work in a pipeline..

    Format-Table returns a collection of formatter objects.  They are meaningless as you are trying to select them.  Once you have a more fundamental understanding of how PowerShell is designed too work this will seem obvious.

    Also to get the output as strings do this:

    gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |
        sort size|
        select -first 3 |
        Format-Table -HideTableHeaders |
        Out-String


    \_(ツ)_/



    • Edited by jrv Thursday, May 21, 2015 11:39 AM
    Thursday, May 21, 2015 11:35 AM
  • when i´m entering this query 

    gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |sort size|select -first 1

    i get this result:

    Partitions : 3
    DeviceID   : \\.\PHYSICALDRIVE0
    Model      : ST3250318AS
    Size       : 250056737280
    Caption    : ST3250318AS


    what i need is the index number of the smallest hard drive. i can find this information in this output.

    in my case 

    gwmi win32_diskdrive -Filter {mediatype='fixed hard disk media'} | select index


    size            index
    ----            -----
    250056737280    0
    500105249280    1
    500105249280    2

    i need only the number 0 for further usage.

    until now i´ve done this with an wmi-query outputted in a file and then sorted. my hope was that its easier with powershell...


    • Edited by logg.sar Thursday, May 21, 2015 11:46 AM
    Thursday, May 21, 2015 11:45 AM
  • That is easy if you start correctly

    This gets the smallest disk:

    gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |
        sort size|
        select -first 1

    This gets the index of the smallest one.

    (gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |
         sort size|
         select -first 3).Index


    \_(ツ)_/

    • Marked as answer by logg.sar Thursday, May 21, 2015 11:55 AM
    Thursday, May 21, 2015 11:50 AM
  • (gwmi win32_diskdrive -Filter 'MediaType="Fixed hard disk media"' |

         sort size|

         select -first 3).Index


    \_(ツ)_/

    thats working. thanks

    and i know - i have still a lot to learn.....

    Thursday, May 21, 2015 11:56 AM
  • Pick any one of the excellent book.  DO a chapter a day and you will be an expert in two weeks.  It is not rocket science but it does take an organized approach to learning the basics.  They cannot be guessed at.

    See: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396


    \_(ツ)_/

    Thursday, May 21, 2015 12:02 PM