Search code examples
windowspowershellpowershell-3.0

Fetching volume number in PowerShell


I am running PS cmdlet get-customcmdlet which is generating following output

Name                         FreeSpaceGB
----                         -----------
ABC-vol001                   1,474.201
ABC-vol002                   2,345.437     
ABC-vol003                   3,147.135
random-value                 4,147.135

I want to capture 003 from the highest volume number ABC-vol003 I also want to ignore random-value and only want to consider the values which have vol in it

get-customcmdlet | select Name 

Name              
----          
ABC-vol001   
ABC-vol001      
ABC-vol001
random-value

Here, I want 003 to be variable based upon highest volume number


Solution

  • You could do a custom sort, and select the last item, like:

    Get-CustomCmdlet | Sort {$_.Name -replace '.*?(\d+)$','$1'} | Select -Last 1
    

    Edit: It looked like you already knew how to use Where, since you had that in your question before you edited it out, but you can use that to only get volumes that have 'vol' in the name, then sort those...

    Get-CustomCmdlet | Where{$_.Name -match '-vol\d+'} | Sort {$_.Name -replace '.*?(\d+)$','$1'} | Select -Last 1