PowerShell access denied for deleting file/folder - powershell

I have a script in PowerShell where I collect old log files, create a zip archive from each file, then delete the original file. Same script is doing folder compression and files compression separately.
While running my script, the files are getting compressed successfully, but when trying to delete the source file I get an access denied error. If I try and delete the file manually in Windows explorer, it gets deleted with no issues.
Most of the files are getting deleted successfully but just for some I get that error.
To delete I use the following command:
get-childitem -Path "mypath" | Remove-item
Tried using -Force and it's still the same.
How can I get around that "access denied" error?

Related

Can delete file with Windows GUI but not with powershell, both using administrator right

I'm trying to delete a machinekey in the folder "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys" via powershell, but get met with the error "You do not have permmision to delete this file.
If I try to delete the same file via Windows gui I'm allowed to do it.
It's the same Admin account I'm using.
This is the code I'm using.
Get-ChildItem -Path "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\19c5cf9c*" | Remove-Item -Force

Powershell - Unable to create directory in Program Files and specify install path

I am trying to write a simple script but I am running into trouble when I try to create a directory in "C:\Program Files".
New-Item -Path "C:\Program Files\JHA" -ItemType Directory
When I do this, no directory is created. No error is thrown. I have admin privileges on my computer. Any advice?
Also, in this script I would like to run an installer and specify this path I created above as the directory it installs to. Is that possible? I read one post where the person was using IMAGEDIR= but someone said Powershell doesn't allow you to specify the installation folder.
Thanks for any help.

Deleting an Empty Directory - Blocked by "It is being used by another process"

I've successfully moved the contents out of a directory into another directory and now I'm attempting to clean up the folder. Upon attempting to delete the folder via Powershell I'm receiving an error "The process cannot access the file because it is being used by another process" even though the folder is now empty. I have the following syntax:
Remove-Item -Recurse -Force pathtodirhere
How can I get around this?

Delete complete folder using a PowerShell script

I am using the following expression to delete a folder from PowerShell. I need to delete a complete folder (including all the files and sub folders).
Remove-Item -Recurse -Force $DesFolder
But this gives me the exception "The directory is not empty"
I am not getting this exception every time when I run the program; it happens randomly. What would be the reason for this and how do I fix this? Because I was failing to reproduce this.
We cannot delete an non-empty directory using commands like rmdir or Remove-Item, this is to avoid accidental deletion of important system files by users during programming.
Therefore before trying to delete the directory, empty it. Clear the contents and then delete it. :)
Remove-Item -Recurse always deletes the directory and all its contents recursively. But it may still fail if directory is modified (i.e. new files are created) by some third-party activity in middle of remove process.
Also, if some of the files cannot be deleted (e.g. due to permission restrictions) Remove-Item will also fail.
So, I'd recommend you to check what exactly is laying inside the directory after exception.

Powershell Copy-Item - Access Denied

I'm writing a script that will open files in a directory and perform a find/replace. But before I do that I would like to make copies of the files in the same directory as the original file. All files exist in the C:\Program Files (x86) directory. The script errors out stating that access to the directory is denied. I see that Copy-Item has a -Credential parameter, but on my test machine I don't have any local Administrator permissions. Doesn't seem like there is going to be any way to copy the file to the same directory. Can I specify the system's temp folder (Windows 7 Professional) and just write it there? I'd like to find a way to copy the file to the same directory as the source file, however.
Windows Vista and above default to not allowing non-administrative users to write to the `%PROGRAMFILES% folder. This means that you're not going to be allowed to copy the files there; you're also not going to be able to save them after doing your find/replace operation.
You can write them to your user documents folder (%USERPROFILE%\Documents) folder instead, if that will work for you.