Question

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.

Was it helpful?

Solution

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)}

OTHER TIPS

$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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top