Search code examples
amazon-web-servicespowershellamazon-ec2windows-server

Powershell can't remove file i just wrote


I wanted to install git on an AWS EC2 Windows Server. So i used an Invoke-WebRequest to download the portable git exe

Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.23.0.windows.1/PortableGit-2.23.0-64-bit.7z.exe -UseBasicParsing -OutFile git.exe

But the Download got stuck and i terminated the session. Now i want to remove git.exe but for some reason i'm not allowed to. I tried removing the file with:

Remove-Item .\git.exe

But i got an error message telling me i'm not allowd to

Remove-Item : Cannot remove item C:\git.exe: Access to the path 'C:\git.exe' is denied.
At line:1 char:1
+ Remove-Item .\git.exe
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\git.exe:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

Solution

  • I just found out that the Invoke-WebRequest was not properly terminated and the stuck process still kept the file on lock. after restarting the server i was able to delete the file. Thanks for the suggestions and comments.