locked
Disk number only from Get-Disk RRS feed

  • Question

  • I'm writing a PowerShell script and I want to get the Number only from Get-Disk.

    Something like this.  

    $DiskNum=Get-Disk|Where-Object–FilterScript{$_.Bustype -Eq"USB"} |Select-ObjectNumber

    However I want $DiskNum to have just the disk number.  At the moment has a table that looks like this Number

                                                                                                                                                          ---------

                                                                                                                                                                 1

    All and any comments greatly appreciated.

    WD   

    Friday, December 12, 2014 10:01 PM

Answers

  • Use 'Select-Object -ExpandProperty Number' on the end.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by tommymaynard Friday, December 12, 2014 10:23 PM
    • Marked as answer by Bill_Stewart Thursday, January 8, 2015 10:00 PM
    Friday, December 12, 2014 10:06 PM

All replies

  • Use 'Select-Object -ExpandProperty Number' on the end.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by tommymaynard Friday, December 12, 2014 10:23 PM
    • Marked as answer by Bill_Stewart Thursday, January 8, 2015 10:00 PM
    Friday, December 12, 2014 10:06 PM
  • So you're aware, you can also do this with dotted-notation in PowerShell 3.0 and greater, which you must be using if you're using the Get-Disk cmdlet.

    $DiskNum = (Get-Disk | Where-Object –FilterScript {$_.Bustype -Eq "USB"}).Number


    Friday, December 12, 2014 10:23 PM
  • Alright.  Both yours and Tommy's solution work just fine.   Although on first try your solution seems a bit faster.  Further testing will be done.

    Thanks guys.

    WD

    Friday, December 12, 2014 11:17 PM