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
ABC-volDAV 4,147.135
ABC-volCDA 5,147.135
I want to capture 003
from the highest volume number
ABC-vol003
I also want to ignore values that don't have have integer value after vol
in it such as ABC-volDAV
and only want to consider the values which have integer value after vol
in it
I am using the following cmdlet but it is generating wrong output ABC-volDAV
Get-CustomCmdlet | Where-Object Name -match vol | select Name | Sort {$_.Name -replace '.*?(\d+)$','$1'} -descending | Select -First 1
My output should be ABC-vol003
Do you mean this?
$inputData = @"
"Name","FreeSpaceGB"
"ABC-vol001","1474.201"
"ABC-vol002","2345.437"
"ABC-vol003","3147.135"
"ABC-volDAV","4147.135"
"@ | ConvertFrom-Csv
$inputData |
Where-Object { $_.Name -match '-vol[\d]+' } |
Sort-Object { [Int] $_.FreeSpaceGB } -Descending |
Select-Object -First 1
The above outputs this:
Name FreeSpaceGB
---- -----------
ABC-vol003 3147.135
Based on the comments - here's another attempt to guess at what the questioner wants to do:
$inputData = @"
"Name","FreeSpaceGB"
"ABC-vol001","1474.201"
"ABC-vol002","2345.437"
"ABC-vol003","3147.135"
"ABC-volDAV","4147.135"
"@ | ConvertFrom-Csv
$inputData |
Where-Object { $_.Name -match '-vol[\d]+' } |
Select-Object `
Name,
@{Name = "Volume"; Expression = { [Regex]::Match($_.Name, '\d+$').Value }},
FreeSpaceGB |
Sort-Object Volume -Descending
This code outputs the following:
Name Volume FreeSpaceGB
---- ------ -----------
ABC-vol003 003 3147.135
ABC-vol002 002 2345.437
ABC-vol001 001 1474.201
So here we create a calculated property (custom column) in the output object containing the volume number (extracted using a regular expression), and sort by that.