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:
$ Copy-Item .\nccert.cer \\hyperv1\c$
Access is denied
+ CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand
What about specifying the -Credential
parameter? That doesn’t work either.
$ Copy-Item .\nccert.cer \\hyperv1\c$ -Credential hyperv1\administrator
The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.
+ CategoryInfo : NotImplemented: (:) [], PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported
And that error pretty much tells me what I need to do: use the New-PSDrive
cmdlet!
$ New-PSDrive -Name H -PSProvider FileSystem -root \\hyperv1\c$ -Credential hyperv1\administrator
$ Copy-Item .\nccert.cer h:\
$ Remove-PSDrive -Name H
[amazon_link asins=’1617294160,1617291161′ template=’ProductCarousel’ store=’benpiperbloginline-20′ marketplace=’US’ link_id=’ff330eeb-fd95-11e7-9367-6b18dc756ddf’]