Created
January 16, 2019 22:41
-
-
Save techthoughts2/2e1fa5d2cfecf53032d17251a39b18d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-FreeDriveLetter { | |
$regex = "[A-Z]:" | |
$netuseOutput = net use | |
$driveArray = @() | |
$providers = Get-PSDrive -PSProvider FileSystem | |
$psdriveDriveLetters = $providers.Name | |
$driveArray += $psdriveDriveLetters | |
$netuseDriveLetters = ($netuseOutput | Select-String -Pattern $regex).Matches.Value | |
$netuseDriveLetters = $netuseDriveLetters -replace ':', "" | |
$driveArray += $netuseDriveLetters | |
$badLetters = ($driveArray) | Select-Object -Unique | |
$exclude = foreach ($badLetter in $badLetters) { | |
[byte][char]$badLetter | |
} | |
$range = 65..90 | |
$Random = $range | Where-Object {$exclude -notcontains $_} | |
$driveLetter = [char](Get-Random -InputObject $Random) | |
Return $driveLetter | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment