1

I'm struggling trying to grab the latest volume/Drive using PowerShell

I have a result of a PowerShell look like this

PS C:\Users\me> Get-WMIObject Win32_Volume | select Name

Name
----
C:\
D:\
E:\
\\?\Volume{021a6bbd-0b97-4973-824a-7c635e362f09}\
\\?\Volume{bae1c1d6-59c3-44b1-9360-b7d3101c0e92}\


PS C:\Users\me>

If I want to access just this

E:

How can I filter out to :\ with the highest alphabetical order ?

I've been trying so many options using Select-String, but seems to get worse result.

code-8
  • 54,650
  • 106
  • 352
  • 604

2 Answers2

2

The ones you want don't start with "\\". The drive letters may be returned in any order, so you need to sort them and take the last one:

Get-WMIObject Win32_Volume | Where-Object {$_.Name -NotLike '\\*'} | select Name | Sort-Object -Property Name | Select-Object -Last 1

Or, if the drive letter is known to be in the range A to Z, then it would be more sensible to use -Like '[A-Z]*' instead of -NotLike '\\*'.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
1

Try something like this

Get-WMIObject Win32_Volume | where {$_.Name -eq "E:\"}

this should give you a list of objects wich you can access like an array. Also there is a lot of useful information here https://technet.microsoft.com/en-gb/library/2007.04.powershell.aspx

modmoto
  • 2,901
  • 7
  • 29
  • 54
  • I only grab the latest one only. I already know how to grab the list. – code-8 Jun 02 '18 at 21:01
  • there is also a sort and a last Sort: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/sort-object?view=powershell-6 Last: https://stackoverflow.com/questions/4546567/get-last-element-of-pipeline-in-powershell – modmoto Jun 02 '18 at 21:04