9

Copy-item cmdlet is not working as expected, i don't understand why. This is my code :

$Source = "C:\folder1"
$Destination = "\\172.22.0.115\c$\folder2\"
$Password  = ConvertTo-SecureString -AsPlainText -Force -String "MyPassword"
$User = "Domain\Administrator"
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password

Copy-Item $Source -Destination $Destination -Credential $credentials 

And this is the error I get :

The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform 
the operation again without specifying credentials.
At C:\Sans titre2.ps1:7 char:1
+ Copy-Item $Source -Destination $Destination -Credential $credentials
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported

I also tried this :

Start-BitsTransfer -Source $source -Destination $Destination -Credential $credentials

And Robocopy doesn't support credentials...

I'm running Powershell V4.0 on Windows 7, and my server is running on Windows server 2012 r2 with powershell V4.0 also.

I want to copy a local folder (with all subfolders) to a remote path \ipadress\c$\folder

How can i resolve it?

Thanks

3
  • What exactly is it that you want to do?
    – MichelZ
    Jun 26 '14 at 15:43
  • @MichelZ I edited my topic
    – Adeel ASIF
    Jun 26 '14 at 15:48
  • Use a User that has access to both shares and use robocopy then?
    – MichelZ
    Jun 26 '14 at 15:49
14
$Source = "C:\folder1"
$Destination = "X:\"
$Password  = ConvertTo-SecureString -AsPlainText -Force -String "MyPassword"
$User = "Domain\Administrator"
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password

New-PSDrive -Name X: -PSProvider FileSystem -Root "\\172.22.0.115\c$\folder2" -Credential $credentials

Copy-Item $Source -Destination $Destination

EDIT: silly error, you can omit the -credential switch on the copy-item cmdlet because you've already done the auth using new-psdrive...

5
  • Thanks. But I'm getting the same error...
    – Adeel ASIF
    Jun 27 '14 at 9:14
  • What error are you getting now? The code above works fine for me Jun 27 '14 at 14:06
  • edited answer... Jun 27 '14 at 14:55
  • Is New-PSDrive creating some sort of temporary network drive that will be removed once the script session has ended?
    – Jim Aho
    Jan 15 '16 at 13:26
  • @JimAho I am not sure whether it will be removed automatically, but you can manually remove it with Remove-PSDrive X (X using the drive letter in the example above; it depends on the drive letter you added of course).
    – Blaise
    Feb 23 at 15:45
-3
  1. Run your PowerShell window as a different user who does have the required permissions, and pull the credentials parts out of your script.

  2. Run your PowerShell/cmd window as a different user who does have the required permissions and use a different utility instead of the PS cmdlets, like robocopy.

2
  • I can't, because this a little part of a big script. And i can't run it as another user.
    – Adeel ASIF
    Jun 27 '14 at 9:15
  • @AdeelASIF Well, 1 and 2 are your two options. I guess we could add failure as option 3. The Copy-Item cmdelt doesn't support the use of the -credentials switch, as the error message explains. So, your only option (other than it not working) is to modify the script so it doesn't use that incompatible switch, and provide the credentials another way. Jun 27 '14 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.