Pick last item from list in Powershell

Try this:

ls function:[d-z]: -n|?{!(test-path $_)} | Select-Object -Last 1

you can just start at the back of the list and go up.

last item: $array[-1] Second to last: $array[-2] and so on.


If you look for a much more verbose, but (in my opinion) readable-improved version:

# Get all drives which are used (unavailable)
# Filter for the "Name" property ==> Drive letter
$Drives = (Get-PSDrive -PSProvider FileSystem).Name

# Create an array of D to Z
# Haven't found a more elegant version...
$Letters = [char[]]([char]'D'..[char]'Z')

# Filter out, which $Letters are not in $Drives (<=)
# Again, filter for their letter
$Available = (Compare-Object -ReferenceObject $Letters -DifferenceObject $Drives | Where {$_.SideIndicator -eq "<="}).InputObject

# Get the last letter
$LastLetter = $Available[-1]

You can use Select-Object -Last 1 at the end of that pipeline.