title: Why is Powershell's Move-Item deleting the destination directory?
body: <p>I need to replace square brackets in filenames, and I've successfully created (and validated at the console) a <code>-replace</code>. Now I'm trying to <code>Move-Item</code> to a new directory because <a href="https://connect.microsoft.com/PowerShell/feedback/details/277707/rename-item-fails-when-renaming-a-file-and-the-filename-contains-brackets" rel="nofollow">this bug in Powershell 2.0</a> prevents me from doing a simple file rename. </p>
  
  <p>Here's my script:</p>
  
  <pre><code>$txtPath = "c:\users\xxxxxx\desktop\cgc\tx"     #source files
  $txtPath2 = "c:\users\xxxxxx\desktop\cgc\tx2"   #renamed files
  Get-ChildItem $txtPath | foreach { 
      Move-Item -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_') 
  }
  </code></pre>
  
  <p>Here's what's happening: I was using the <code>$txtPath2</code> variable, but kept getting "cannot bind to null directory" errors, so I explicitly coded to the path to see if there was something odd with how the variable parsed. Now, I get this error:</p>
  
  <pre><code>Move-Item : Cannot move item because the item at 'C:\users\xxxxxx\desktop\test001' does not exist.
  At C:\users\xxxxxx\desktop\cgc\rni.ps1:5 char:10
  + Move-Item &lt;&lt;&lt;&lt;  -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_')
      + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
      + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand
  </code></pre>
  
  <p>Here's what's odd: I create the new directory. I run the script, watch it vanish from my desktop as the script fails. WTF? I've quit and restarted the console application to flush any variables. I've tried different flavors of variable vs. constant in the <code>Move-Item</code> line. Unless there's a <code>Move-Item</code> parameter that I'm missing, I really have no idea what's going on. Does anyone else see anything that would cause my file to be deleted?</p>
  
  <p>EDIT:
  After editing to </p>
  
  <pre><code>Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($i.FullName -replace '\[|\]', '') ) }
  </code></pre>
  
  <p>I get a new error:</p>
  
  <pre><code>Exception calling "Move" with "2" argument(s): "Empty file name is not legal.
  Parameter name: destFileName"
  At C:\users\x46332\desktop\cgc\rni.ps1:6 char:52
  + Get-ChildItem $txtPath | % { [system.io.file]::Move &lt;&lt;&lt;&lt; ($_.fullname, ($i.FullName -replace '\[|\]', '') ) }
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException
  </code></pre>