GibboK
GibboK

Reputation: 73938

PowerShell error: Cannot move item because the item at xxx is in use

I am using PowerShell, I need to Move-Item a folder after some operation is executed (in my case delete all empty folders).

When I run the following script I get an error:

Move-Item : Cannot move item because the item at 'C:\Projects\xxx\aaa' is in use.
At line:58 char:1
+ Move-Item $dist $fin
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand

Any idea how to solve this?

# delete all empty folder recursively at any levels
do {
  $dirs = gci $tdc -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
  $dirs | Foreach-Object { Remove-Item $_ }
} while ($dirs.count -gt 0)

# move folder
Move-Item $dist $fin
Write-Host '- Moving files executed.'

Upvotes: 2

Views: 3437

Answers (1)

Elijah Brown
Elijah Brown

Reputation: 41

You need to move out of the directory that you are working in.

cd ~

Then use the Move-Item command

Upvotes: 4

Related Questions