1

I have a powershell script that needs to rename its parent directory:

$currDir = pwd
Rename-Item $currDir "NewName"

When I run this, I get the error "Rename-Item : The process cannot access the file because it is being used by another process." I've also tried (to be sure it isn't because the current directory is set to the target directory):

$currDir = pwd
cd ..
Rename-Item $currDir "NewName"
cd "NewName"

It still gives me the same error. It seems to be doing this if the script resides inside the directory, regardless of what directory I cd into in the script. How can I get around this? I realize some answers will probably say "move the script outside the directory", but I'd really like to find a real solution if possible.

arknotts
  • 454
  • 3
  • 13
  • The first thing you tried won't work, but the second script does... You sure your getting an error? if so there is more to this script that needs to be posted. – Cole9350 Jul 17 '14 at 13:40
  • OK so it's working in PowerShell ISE just fine. It breaks when I right click and "Run with Powershell" through Explorer. I have a feeling this is because the Explorer window is open in the current directory. – arknotts Jul 17 '14 at 14:01

1 Answers1

0

You won't be able to rename the parent directory if the script is executing from it, windows locks that directory because a child item is open within it. Any reason you can't move the script?

joshduffney
  • 389
  • 1
  • 4
  • 16
  • I'm looking to rename a directory with a wildcard but have been unsuccessful. Tried Rename-Item "^Folder*" $value but unsuccessful – Princy Jun 10 '21 at 19:57