Copy-Item error from J Helmick course

Welcome Forums General PowerShell Q&A Copy-Item error from J Helmick course

Viewing 2 reply threads
  • Author
    Posts
    • #49196
      Participant
      Topics: 120
      Replies: 258
      Points: 229
      Rank: Participant

      I was runningt hrough examples from Jason's Lynda.com course on PS 5 and get an error trying to use Copy-Item in a ForEach-Object loop

      PS C:\> $servers | foreach-object  {Copy-Item -Credential $creds .\cl21163_Groups.txt -Destination \\$_\C$\ }
      The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying
      credentials.
      At line:1 char:29
      + ... ch-object  {Copy-Item -Credential $creds .\cl21163_Groups.txt -Destin ...

      What am I doing wrong here? His example doesn't require creds but my servers do. I have my creds stored in a variable.

      thanks

    • #49200
      Participant
      Topics: 1
      Replies: 169
      Points: 0
      Rank: Member

      To use credentials you will need to authenticate and create a temporary mapped drive to the remote system.
      Something like this should work for you

      $servers | foreach-object  {
          New-PSDrive -Name Copyto -PSProvider FileSystem -Credential $creds -Root "\\$_\C`$\" 
          Copy-Item -Path .\cl21163_Groups.txt -Destination Copyto:\
          Remove-PSDrive -Name Copyto
      }
      
    • #49202
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      Hi Jeff,

      A possible solution would be to use the New-PSDrive cmdlet with credentials before the Copy-Item cmdlet.

      Best,
      Daniel

Viewing 2 reply threads
  • The topic ‘Copy-Item error from J Helmick course’ is closed to new replies.