简体   繁体   中英

In Powershell, how do I delete a file with a hyphen (dash) in the filename?

In Powershell, when I try to delete a file with a hyphen in the nam like this:

remove-item 'C:\S3\op_netadmin-47.bak'

I get this error:

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

But if I rename the file to op_netadmin_47.bak, it works fine.

I've tried with and without double quotes as well as single quotes

I tried your file name with - and _ , both work ok. I also tried to use " around file name which also works.

As @mklement0 mentioned in comments read-only files require -Force parameter

Make sure user running script has read/write access to given folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM