title: Powershell/Batch network mapped drive
body: <p>I want a powershell/batch script that map a drive on a free letter. Or test letters then map on a free one.</p>
  
  <p>I'm sure there is something easier than this.</p>
  
  <pre><code>$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] &gt; $null){
          Write-Host $tab[$i] "`tExists"
      }
      else{
          Write-Host $tab[$i] "`tDoesn't exist"
      }
  }
  </code></pre>
  
  <p>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</p>
  
  <p><code>$freeletter | Output-File -Append ....</code></p>
  
  <p>or 2) my .bat script does something like that :</p>
  
  <pre><code>Is G: free ?
     No Is H: free ?
          Yes : Map It -&gt; $mapdrive_1 = "H:"
                Is I: free ?
                      No  Is J: Free ?
                                Yes : Map it -&gt; $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
  </code></pre>
  
  <p>Hope you understand what I mean :)</p>
  
  <p>Regards,</p>
  
  <p>Nico.</p>