2013-08-02 3 views
2

d부터 z까지 사용할 수있는 드라이브 목록을 제공하는이 코드 줄을 사용하여 드라이브 문자를 매핑하려고합니다.Powershell의 목록에서 마지막 항목 선택

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

목록에서 임의의 문자가 아닌 마지막 문자를 선택하고 싶습니다. 그 일을 어떻게 하죠? Powershell을 처음 사용하여 도움을 주셔서 감사합니다.

답변

6

해당 파이프 끝에 Select-Object -Last 1을 사용할 수 있습니다.

+0

감사, 완벽한 작품! – Nimjox

2

이 시도 :

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

Get-Psdrive을 구문 분석 D-Z의 모든 경로를하려고하고 필요로하지 않는 다른 옵션을 선택합니다. 당신이 훨씬 더 자세한 정보를 찾을 경우

$lettersInUse = Get-Psdrive | ? { $_.Name.Length -eq 1 } | % { $_.Name } 
$lastDriveLetter = [Char]'Z' 
while ($lettersInUse -contains $lastDriveLetter) { 
    $lastDriveLetter = [Char]($lastDriveLetter - 1) 
} 
$lastDriveLetter 
4

하지만, (내 생각에) 읽을 수-개선 된 버전 : 예를 들면 다음과 같습니다이다

# 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] 
+2

나는 이것을 좋아한다 :'$ letters = 69..90 | % {[char] $ _}' –

+2

@ C.B :'[char []] (69..90)'로 훨씬 쉽게 할 수 있습니다. 파이프 라인을 간단한 캐스트로 드래그 할 필요가 없습니다. – Joey

+0

@ Јοеу 감사합니다! 나는 내가 더 좋아하는 것을 발견했다.) –

관련 문제