# \[SOLVED\] - Copy-item Error remote localhost

**URL:** <https://forums.powershell.org/t/solved-copy-item-error-remote-localhost/21171>\
**Category:** PowerShell Help\
**Created:** [December 15, 2022, 11:55am UTC](https://forums.powershell.org/t/solved-copy-item-error-remote-localhost/21171 "2022-12-15T11:55:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axel](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Axel](https://forums.powershell.org/u/Axel)\
**Post date:** [December 15, 2022, 11:55am UTC](https://forums.powershell.org/t/solved-copy-item-error-remote-localhost/21171/1 "2022-12-15T11:55:50Z")

</div>

Hello,  
I have a problem when I want to copy a file from one server to another.  
The copy of the data is done with an authentication and when I run the script, I get the following error message:  
Connection to the remote server localhost failed with the following error message: Access denied  
I have run the command “Enable-PSRemoting -Force” but it does not work.  
Here is my script

```auto
$username = "username"
$password = "password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$Session = New-PSSession -Credential $cred
$remote_share="\\srv\remote_share"
$local_share="F:\tshare"
copy-item $local_share $remote_share -Fromsession $session -recurse -force

```

Can someone please help me?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 15, 2022, 1:10pm UTC](https://forums.powershell.org/t/solved-copy-item-error-remote-localhost/21171/2 "2022-12-15T13:10:30Z")

</div>

Axel,  
Welcome to the forum. 👋🏽

Is this supposed to be an automatically running task or do you want to run it manually?

Regardless of that …

Do you want to copy the item from the computer you run this script on **TO** another computer, right? Then you should use the parameter `-ToSession` instead of `-FromSession`.

If you use the parameter `-FromSession` or `-ToSession` you use a local path for both parameters … `-Path` AND `-Destination`.

If this is supposed to be an automatically running task I’d recommend using robocopy and set the task to be run with the context of the user with access to the target folder. THat’s the easiest and probably most robust way.  
Another simply and very straight forward way would be to grant access to the user you run this script with.

---

<div class="post-metadata">

**Author:** ![Axel](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Axel](https://forums.powershell.org/u/Axel)\
**Post date:** [December 15, 2022, 4:42pm UTC](https://forums.powershell.org/t/solved-copy-item-error-remote-localhost/21171/3 "2022-12-15T16:42:09Z")

</div>

Hello Olaf,  
Thanks for your welcome  
but in fact, I found out where my problem came from. I forgot to tell my user that he had the possibility to execute the script.  
To do so, you have to execute the command:

`Set-PSSessionConfiguration -ShowSecurityDescriptorUI -Name Microsoft.PowerShell`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/solved-copy-item-error-remote-localhost/21171/4 "2024-05-16T20:16:16Z")

</div>


