0

I run this PowerShell script, and it works fine on PowerShell 4.0. But I now have PowerShell 5.0 and the script does work but it throws an error:

The Script:

$path = "X"
$destination = "Y"

while (Test-Path -Path $path) {
    Move-Item -Path "$path\*zip" -Destination "$destination"
    }

The error I get is:

Move-Item : The process cannot access the file because it is being used by another process.

G42
  • 9,791
  • 2
  • 19
  • 34
Djbril
  • 745
  • 6
  • 26
  • 48

2 Answers2

1

The title of the question: "Test-Path Move-Item Problems" implies that one cmdlet might be impacting the other. That doesn't make sense to me as Test-Path is checking the folder's existence and Move-Item is working on child items within that folder.

Personally I would not use a while loop for this use case as, once you have determined that the path exists you don't need to keep testing it:

if(Test-Path -Path $path){
  Move-Item -Path $path\*zip -Destination $destination
}
Charlie Joynt
  • 4,411
  • 1
  • 24
  • 46
0

just do it

 Move-Item -Path "$path\*zip" -Destination "$destination" -ErrorAction Ignore
Esperento57
  • 16,521
  • 3
  • 39
  • 45