Home > Enterprise >  Powershell: Get all the available Drive letters
Powershell: Get all the available Drive letters

Time:07-24

Using Powershell I am seeking the inverse of the drive letters currently in use.

The following code In a .bat-file reveals the currently available drives.

SETLOCAL EnableDelayedExpansion
net use
SET DRVLST=
FOR %%p IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO if not exist %%p:\nul @set DRVLST=!DRVLST! %%p:
echo Available Drives: %DRVLST%
echo -------------------------------------------------------------------------------
net view %SERVERNAME%
ENDLOCAL

In Powershell the drive letters in use can be extracted and stored in a variable $drives_in_use for further processing.

$drives_in_use=(Get-PSDrive).Name -match '^[a-z]$'
write-host $drives_in_use

How to obtain the result to be the 'subtraction' of $drives_in_use from all drives A to Z?

$availableDrives = $allDrives - $drives_in_use

I would assume that a reverse-lookup regex might be the solution. But I don't know how to achieve that.

CodePudding user response:

One approach would be to create an alphabet array and filter with the notcontains operator.

#Create alphabet array (https://www.rapidtables.com/code/text/ascii-table.html)
$alph = 65..90 | ForEach-Object {[char]$_}
$availableDrives = $alph | Where-Object { $drives_in_use -notcontains $_ }

If you're using PowerShell 6 then you can create $alph like this

$alph = 'A'..'Z'

CodePudding user response:

Another approach would be to use .Net like:

$availableDrives = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -split '' | Where-Object { $_ -notin ([System.IO.DriveInfo]::GetDrives().Name).Substring(0,1) }

or

$availableDrives = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.ToCharArray() | Where-Object { $_ -notin ([System.IO.DriveInfo]::GetDrives().Name).Substring(0,1) }

or

$availableDrives = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -split '' | Where-Object { $_ -notin [System.IO.Directory]::GetLogicalDrives().Substring(0,1) }

or use Get-CimInstance (slower)

$availableDrives = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -split '' | Where-Object { $_ -notin (Get-CimInstance -ClassName win32_logicaldisk).DeviceID.Substring(0,1) }
  • Related