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:<br /> $ Copy-Item .\nccert.cer \\hyperv1\c$<br /> Access is denied<br /> + CategoryInfo          : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException<br /> + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand<br />

What about specifying the -Credential parameter? That doesn’t work either.<br /> $ Copy-Item .\nccert.cer \\hyperv1\c$ -Credential hyperv1\administrator<br /> The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.<br /> + CategoryInfo          : NotImplemented: (:) [], PSNotSupportedException<br /> + FullyQualifiedErrorId : NotSupported<br />

And that error pretty much tells me what I need to do: use the New-PSDrive cmdlet!<br /> $ New-PSDrive -Name H -PSProvider FileSystem -root \\hyperv1\c$ -Credential hyperv1\administrator<br /> $ Copy-Item .\nccert.cer h:\<br /> $ Remove-PSDrive -Name H<br />

_Ready to beef up your PowerShell skills? Get unlimited access to every course in Pluralsight’s online training library!_

[amazon_link asins=’1617294160,1617291161′ template=’ProductCarousel’ store=’benpiperbloginline-20′ marketplace=’US’ link_id=’ff330eeb-fd95-11e7-9367-6b18dc756ddf’]