如何在 PowerShell 中复制只读和隐藏文件/文件夹?
powershellmicrosoft technologiessoftware & coding更新于 2026/2/15 15:52:17
要将只读和隐藏项目从一个位置复制到另一个位置,您需要在 Copy-Item cmdlet 中使用 –Force 参数。
如果针对只读/隐藏文件运行该命令时未使用 Force 参数,则会出错。以下给出了一个示例。
示例
Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\
输出
PS C:\WINDOWS\system32> Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\ Copy-Item : Access to the path 'D:\TempContent\Readonlyfile.txt' is denied. At line:1 char:1 + Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (D:\Temp\Readonlyfile.txt:FileInfo) [Copy-Item], UnauthorizedAccessException + FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
在 cmdlet 中添加 –Force 参数后,它也可以复制只读/隐藏文件。
示例
下面给出了一个只读文件的示例。
Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\ -Force -PassThru
输出
PS C:\WINDOWS\system32> Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\ -Force -PassThru Directory: D:\TempContent Mode LastWriteTime Length Name ---- ------------- ------ ---- -ar--- 13-01-2020 18:19 0 Readonlyfile.txt
示例
以下示例针对隐藏文件。
Copy-Item D:\Temp\hiddenfile.xlsx -Destination D:\TempContent\ -Force -PassThru
输出
PS C:\WINDOWS\system32> Copy-Item D:\Temp\hiddenfile.xlsx -Destination D:\TempContent\ -Force -PassThru Directory: D:\TempContent Mode LastWriteTime Length Name ---- ------------- ------ ---- -a-h-- 13-12-2019 09:52 6182 hiddenfile.xlsx

