title: Why is my PowerShell Code deleting my folder when I am trying to move files over?
body: <p>In Powershell, I am trying to move all files that are 2 years and older to a different directory.</p>
  <p>But my code doesn't move my files, deletes the destination folder, and says that the destination folder doesn't exist.</p>
  <p>I know that this works when it is not in a function, but I need it to be in a function because I am dealing with multiple directories where I shouldn't need to replicate code.</p>
  <pre><code>Function Move ($oldPath, $years, $newPath)
  {
      &lt;#Moving File#&gt;
      $years = $years * -1
      $date = [DateTime]::Now.AddYears($years)
      Foreach($file in (Get-ChildItem $oldPath))
      {
          if($file.LastWriteTime -eq $date -or $file.LastWriteTime -lt $date)
          {
              Move-Item -Path $file.fullname -Destination $newPath
          }
  
      }
  }
  $oldPath = insert source file path here
  $years = 2
  $newPath = insert destination file path here
  Move ($oldPath, $years, $newPath)
  </code></pre>