如何在PowerShell中复制只读和隐藏文件/文件夹?
要将只读项目和隐藏项目从一个位置复制到另一位置,需要将–Force参数与Copy-Itemcmdlet一起使用。
当您为只读/隐藏文件运行不带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热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短