title: Copying from one server to other with credentials
body: <p>I'm trying to copy a file from one server to another server so that it doesn't ask me to enter my login info and I don't have to store the password blatently in the code.  The problem is, it's still coming up with a credential request GUI, even though I give the -Credential parameter.  This is how I store the password in a file:</p>
  
  <pre><code>read-host -assecurestring | convertfrom-securestring | out-file C:\secString.txt
  </code></pre>
  
  <p>Then the code looks like this:</p>
  
  <pre><code>function GetSecureLogin() {
     $username = "usa02xxxswg2\20xbackup"
     $password = get-content C:\secString.txt | convertto-securestring
     $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
  }
  
  function CopyFileToFolder ([string]$Source, [string]$destination){
     Copy-Item "$Source" -Destination "$destination" -Credential $cred  #this is where the login info box is coming up but shouldn't be
  }
  
  ######################start here#####################
  $cred = ""
  GetSecureLogin
  
  $tempSource = "filename.txt"
  $ToLocation = "$ToLoc"
  CopyFileToFolder $tempSource $ToLoc
  </code></pre>
  
  <p>Any ideas?  I'm relatively new to PowerShell, so I could be doing something silly.  I'm not sure how to tell if $cred is getting back to my Copy function ok or if it's out of scope.  </p>
  
  <p>I've looked at these examples but I'm having trouble deciding if I need to do something different because they are specifying the server info and I'm trying to avoid that since I just want to use UNC path like I was doing before.  The copying was working fine without login because I was using my Active Directory login to save the task.  Now we want to use a locally saved login (where the script is run) and then it needs a different local login when it's trying to access the other server.</p>
  
  <p><a href="https://stackoverflow.com/questions/612015/copy-item-with-alternate-credentials">copy-item with alt cred</a>, 
  <a href="https://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password">powershell without prompt for cred</a>, 
  <a href="https://stackoverflow.com/questions/13239377/how-can-i-copy-from-local-to-a-remote-location-with-credentials-in-powershell">copy without cred</a>, 
  <a href="http://powershell.org/wp/forums/topic/copy-with-alternate-credentials/" rel="nofollow noreferrer">copy with alt cred</a>, 
  <a href="https://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password">cred without password prompt</a></p>