0

I've got a PowerShell script to copy a bunch of files to a directory in Program Files (x86). The script itself loops through a list of files and calls

Copy-Item -Path $src -Destination $dst -Verbose -Confirm:$false -Force

for each one, but when it comes to overwriting a file, I get an access denied error on the path. The PowerShell script is run from a batch file which I'm running as administrator, so I'm not sure why this is failing. I've checked all the paths in question and they're all correct.

The script is running on Windows Server 2012, and ran successfully when I last ran it a few months ago. No user permissions have changed since then.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Mourndark
  • 2,526
  • 5
  • 28
  • 52
  • 2
    What happens if you try to overwrite this file manually using Windows Explorer? My guess is that it has security permissions and ownership set to a system account like e.g. TrustedInstaller. See if this is the case in the file's properties->security->advanced. – wOxxOm Aug 05 '15 at 11:49
  • 2
    Check if the file is being used while you copy items, if a file is open by some process, overwriting it would fail regardless of permissions. – Vesper Aug 05 '15 at 11:51
  • Copying the file in Windows Explorer works fine strangely. I've just checked the files in question, they're all owned by `SYSTEM`. – Mourndark Aug 05 '15 at 11:59
  • 1
    Could you maybe use handle to see if another process is locking the files? https://technet.microsoft.com/en-gb/sysinternals/bb896655.aspx – Shovers_ Aug 05 '15 at 12:28
  • 1
    You can just [try opening the file for writing](http://stackoverflow.com/questions/24992681/powershell-check-if-a-file-is-locked) to test if it's currently locked. – Bacon Bits Aug 05 '15 at 13:59
  • It looks like the act of manually overwriting the files in Explorer freed them up so now my script runs fine. It's still a mystery as to why they were locked to an administrator in PowerShell but not to the same Administrator in Explorer though. Looks like this will need some more investigation with Handle. – Mourndark Aug 07 '15 at 15:52

0 Answers0