Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Sort Physical Disks by FriendlyName

$
0
0

No, because "11" will sort before "2." A "plain" sort will only work if it's something like "PhysicalDisk01" and so forth.

If that's not the case, I would probably do the Select-Object first, adding a custom property that contains only the disk sequence number, as an integer. THEN I'd sort on that custom property. So, similar to what you're doing, but reversing the logic a bit. Doing it my way will also make it easier to see if your "messy syntax" is working the way you think it is ;).

But you're overdoing the syntax a bit. For example…

[int]($expression)

Will cast something as an integer; you don't need all the Parse() method stuff. You can also get rid of "PhysicalDisk" more easily.

[int]($_.FriendlyName.Replace('PhysicalDisk',"))

Basically, just remove "PhysicalDisk" by replacing that string with an empty string. That'll leave you with a numeric string, which is then case as an integer. You shouldn't HAVE to pull a ToString() on it first, since the property value should already be a string. The ISE may not "see" it as a string as you're typing, but it ought to run.


Viewing all articles
Browse latest Browse all 13067

Trending Articles