1

I want a powershell/batch script that map a drive on a free letter. Or test letters then map on a free one.

I'm sure there is something easier than this.

$tab = @()

$tab = @('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')

for($i = 0;$i -lt 26;$i++){

    if(net use $tab[$i] > $null){
        Write-Host $tab[$i] "`tExists"
    }
    else{
        Write-Host $tab[$i] "`tDoesn't exist"
    }
}

I need my PS script to write something in a .bat script. So, 1) in my PS script find free letters then write it to the .bat script like this

$freeletter | Output-File -Append ....

or 2) my .bat script does something like that :

Is G: free ?
   No Is H: free ?
        Yes : Map It -> $mapdrive_1 = "H:"
              Is I: free ?
                    No  Is J: Free ?
                              Yes : Map it -> $mapdrive_2 = "J:"

#Once it's done I got my ONLY 2 mapped drives
$mapdrive_1
$mapdrive_2

#then i can do
net use $mapdrive_1 "my\path1"
net use $mapdrive_2 "my\path1"
start robocopy $mapdrive_1 $mapdrive_2 /options
net use $mapdrive_1 /DELETE
net use $mapdrive_2 /DELETE

net use $mapdrive_1 "my\path2"
net use $mapdrive_2 "my\path2"
start robocopy $mapdrive_1 $mapdrive_2 /options
net use $mapdrive_1 /DELETE
net use $mapdrive_2 /DELETE

Hope you understand what I mean :)

Regards,

Nico.

1
  • with cmd.exe, you can use net use \\servername\share this will map the network drive for you using the first free letter. Popd will remove it afterward. Commented Jul 9, 2013 at 13:31

2 Answers 2

4

This will output all free letters, you can then pipe to select-object to get just the first one:

ls function:[a-z]: -name | where {-not (get-psdrive $_[0] -ea 0)}
3
  • This is just incredible :D Really impressive ! I've just find another solution, but so much longer than yours ^^ with Get-PSDrive | ?{$_.Root -eq $drive + ":\"} and If($list.Root -ne $drive + ":\") And my bad, i HAVE to do it in batch, because the batch script will be executed as last. A collegue tell me that net use * \my\path1 /PERSISTENT:NO will map a drive without letter, and i'll be able to delete it with net use * \my\path1 /DELETE Can you confirm this ? :) Thank you @ShayLevy for your clever answer Nico.
    – Nico
    Commented Jul 9, 2013 at 12:51
  • As far as, in case of *, a letter will be selected for you but it isn't necessarily the first one available..
    – Shay Levy
    Commented Jul 9, 2013 at 13:12
  • Yeah, and if I just remove the * it will mount a temporary drive, without any letter. Thank you alot @ShayLevy
    – Nico
    Commented Jul 9, 2013 at 13:26
0
$Mapdrive = "\\DRIVE NAME\My\Path\"

Using a drive alias as I used about makes the drive letter irrelevant which is particularly handy if for some reason or another the drive letter changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.