title: Powershell: How to repeat a condition until a valid value is passed?
body: <p>I am working on a script that clones a directory structure (excluding files) to a new directory. Here's my code so-far:</p>
  <pre><code>Function Copy-DirectoryStructure{
      [CmdletBinding()]
  
      Param(
          [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
          [string]$Path
      )
  
      # Test whether the path is exactly a drive letter
      if($Path -match '^[a-zA-Z]:\\$' -or $Path -match '^[a-zA-Z]:$'){
          throw &quot;Path cannot be at the root of a drive. Aborting.&quot;
      }
  
      # Check if the path is valid
      if(Test-Path -Path $Path -PathType Container){
          # Create the destination path format
          $DestinationPath = (get-item $Path).FullName + ' Folder Copy'
          # Test if our destination already exists.
          # If so, prompt for a new name
          if(Test-Path -Path $DestinationPath -PathType Container){
              Write-Warning &quot;The destination path already exists.&quot;
              $NewFolderName = Read-Host -Prompt &quot;Input the name of the new folder to be created.&quot;
              $DestinationPath = (get-item $Path).parent.FullName + '\' + $NewFolderName
          }
          # Begin copy
          robocopy $Path $DestinationPath /e /xf *.* | Out-Null
      } else {
          throw &quot;Invalid directory was passed. Aborting.&quot;
      }
  }
  
  Copy-DirectoryStructure -Path &quot;C:\Users\[username]\Desktop\Test&quot;
  </code></pre>
  <p>The relevant part is here:</p>
  <pre><code># Create the destination path format
          $DestinationPath = (get-item $Path).FullName + ' Folder Copy'
          # Test if our destination already exists.
          # If so, prompt for a new name
          if(Test-Path -Path $DestinationPath -PathType Container){
              Write-Warning &quot;The destination path already exists.&quot;
              $NewFolderName = Read-Host -Prompt &quot;Input the name of the new folder to be created.&quot;
              $DestinationPath = (get-item $Path).parent.FullName + '\' + $NewFolderName
          }
  </code></pre>
  <p>Right now it prompts the user to input a new name if $DestinationPath already exists and then proceeds to the robocopy. But what if the user inputs a folder name that ALSO already evaluates to an existing path?</p>
  <p>I want to handle this scenario gracefully and re-prompt for a new path until the user enters a destination path that doesn't already exist.</p>
  <p>I have no idea how to do this.</p>
  <p>I know this is an extreme edge-case, but I want to make my code as safe as possible.</p>
  <p>Any help is greatly appreciated.</p>