首页 > 解决方案 > 在PowerShell中将文件复制到需要管理员权限的文件夹

问题描述

我想c:\windows\system32\drivers\etc通过 powershell 脚本将主机文件复制到,但是当我这样做时,出现以下错误。如何在不显示密码的情况下以管理员身份运行此命令?

我收到的错误:

copy : Access to the path 'C:\Windows\System32\drivers\etc\hosts' is denied.
At line:1 char:1
+ copy z:\hosts C:\Windows\System32\drivers\etc
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (Z:\hosts:FileInfo) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

我运行的命令:

copy z:\hosts C:\Windows\System32\drivers\etc 

标签: powershell

解决方案


If you have administrator permissions, then you start PowerShell as administrator, or you can run the script as an administrator by adding,

Start-Process powershell -Verb runAs


推荐阅读