title: Check shared drive is already mapped
body: <p>I'm using PowerShell for the first time to check if a unc path is already mapped, and if it is delete it to create it again under specific credentials.</p>
  
  <p>The problem I have found is if the path exists in the net use list but has a drive letter, you have to delete it by the drive letter which can be of course random.  So I need to find out the drive letter when I match it but I am clueless as to how.</p>
  
  <pre><code>$userPass = '/user:Domain\User password';
  $ltr = ls function:[d-z]: -n | ?{ !(test-path $_) } | random;
  $share = '\\\\server\\folder';
  $newMap = 'net use '+ $ltr + ' '$share + ' '$userPass;
  foreach ($con in net use) {
      if ($con -match $share) {
          $MappedLetter = /*something here to find the matched drive?*/
          if ($MappedLetter) {
              net use $MappedLetter /delete
          } else {
              net use $share /delete
          }
      }
  };
  
  net use $newMap;
  </code></pre>
  
  <hr>
  
  <p><strong>[Edit]</strong></p>
  
  <p>I have tried the <code>Get-PSDrive</code> but this only works IF the UNC path is mapped to a drive.  As these can at times be a "zombie", i.e. exist in <code>net use</code> but have no letter, this method won't always work.  I can combine the two methods (above an <code>Get-PSDrive</code>) but if anyone has a cleaner way please let me know!</p>