如何在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热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下