Fixing PowerShell’s Copy-Item “Access is Denied” Error
Recently I needed a way to copy a certificate file from within a PowerShell session to another Windows machine without opening a nested PowerShell session. But I ran into a little snag along the way: Copy-Item
‘s dreaded Access is denied
error.
Here’s my setup
- A Windows 10 laptop, from which I’m remoting
- NC1, a Server 2016 virtual machine I’m remoted into. It’s a member of a domain.
- HYPERV1, the Server 2016 machine I want to copy a certificate file to. It’s not a member of a domain.
I execute all of the following commands on NC1, the VM I’m remoted into.
Here’s the first thing I tried. The HYPERV1 machine is not a member of a domain, so the following doesn’t work:
1$ Copy-Item .\nccert.cer \\hyperv1\c$
2Access is denied
3+ CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand
What about specifying the -Credential
parameter? That doesn’t work either.
1$ Copy-Item .\nccert.cer \\hyperv1\c$ -Credential hyperv1\administrator
2The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.
3+ CategoryInfo : NotImplemented: (:) [], PSNotSupportedException
4+ FullyQualifiedErrorId : NotSupported
And that error pretty much tells me what I need to do: use the New-PSDrive
cmdlet!
1New-PSDrive -Name H -PSProvider FileSystem -root \\hyperv1\c$ -Credential hyperv1\administrator
2Copy-Item .\nccert.cer h:\
3Remove-PSDrive -Name H