PowerShell Problem Solver: Copy Files with Alternate Credentials

Posted on October 15, 2014 by Jeff Hicks in PowerShell with 0 Comments

Not too long ago, I chimed in on a PowerShell problem about copying files on some social media platform. An IT pro was facing a challenge using PowerShell to get his job done and wasn’t sure what to do. The task at hand was to copy files to a network location using alternate credentials. Naturally, he wanted this accomplished using PowerShell. At first glance, you might think this should be pretty easy. If you look at help for Copy-Item, you will see a credential parameter. However, if you look at help for this parameter, then you will come across a rather stunning pronouncement.

The credential parameter is not supported in Windows PowerShell. (Image Credit: Jeffery Hicks)

The credential parameter is not supported in Windows PowerShell. (Image Credit: Jeffery Hicks)

Yes. This parameter is not used out-of-the-box. This is why it is important that you be in the habit of reading full help in PowerShell. More than likely this parameter exists for future providers or future versions of existing providers that will support it. But for right now in PowerShell 4.0, this is not an option. So if we can’t use an alternate credential, how can we copy files?

My solution is to leverage the New-PSDrive cmdlet. This cmdlet has a credential parameter that will work. I should be able to create a new drive to a network share using an alternate credential.

Now I can copy files to this destination.

When I’m finished copying I can remove the PSDrive.

But it would sure be nice to make this process more transparent. So I created a proxy function for Copy-Item. A proxy function is wrapper for a PowerShell command that you can customize. Often proxy commands add or remove parameters to an underlying cmdlet. I used a function that I published on my blog to create a proxy version of Copy-Item. Here is a version that I call Copy-Item2 because I didn’t want to overwrite the original Copy-Item2, although you certainly could.

I modified the credential parameter to include a validation test, verifying that the destination is a UNC. If it isn’t, then PowerShell will throw an exception when you run the command.

In the script, if –Credential is specified, I want to create a temporary PSDrive. Because I don’t want to have to guess what might be an available drive letter or name, I’ll have the .NET Framework give me random file name which will be something like ‘3bani1qf.gie’. Then I get the filename only, i.e. ‘3bani1qf’.


All of this done in a one-line command:

Armed with a name, I can create the PSDrive.

Now, because Copy-Item is a wrapped command, it doesn’t really use Copy-Item, I don’t want it to see the credential parameter, so I’ll drop it from the bound parameters.

I will also redefine the destination parameter to use the newly created PSDrive.


The proxy function will invoke the underlying cmdlet with the defined parameters.

With this function loaded in my PowerShell session I can use Copy-Item, or at least my version, as you would expect:

Hopefully if you use this command it will always work. But in my testing it would never work 100% of the time. You might see an error about multiple connections to a server not being allowed. I’ll have another alternative next week.


Tagged with , , ,