# Copy file from Users AppData Folder to Server Folder

**URL:** <https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501>\
**Category:** PowerShell Help\
**Created:** [May 3, 2019, 5:39am UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501 "2019-05-03T05:39:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![smack2k](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@smack2k](https://forums.powershell.org/u/smack2k)\
**Post date:** [May 3, 2019, 5:39am UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501/1 "2019-05-03T05:39:03Z")

</div>

Looking for some assistance…brand new to the forums and farily new to Powershell

I am trying to create a Powershell script that will allow me to go out to Users Computers (from a list of computers I have in a text file) and pull a few xml files from their C:\Users\UserAppData\Roaming\ folder and copy them to a folder on the server with the name of the computer (if possible).

I have the basics of my script down (variable to grab the computer names from my list and the general command to Copy-Item from one path to another on the server for each file, but where I need assistance is in how to tell the script (Via variable?) to look for each user on each computer and grab the files as well as how to tell the script to create a folder under the server share I created for that computer name and copy the files there…or even one step further and create subfolders under the created computername folder for each username on the system…

Any assistance is greatly appreciated…here is what I have so far. I have a Copy-Item for each file (there are two ) I need from each users folder. Trying to have a folder created under XML Backups for each ComputerName with possible subfolders for each user on that computer…

[pre]

$computers = Get-Content “C:\Computers.txt”

foreach ($computer in $computers) {  
Invoke-Command -ComputerName $computer -ScriptBlock {Copy-Item -Path C:\Users\User\AppData\Roaming\FolderName\file.xml -Destination '\servershare\XML Backups'}

Invoke-Command -ComputerName $computer -ScriptBlock {Copy-Item -Path C:\Users\User\AppData\Roaming\FolderName\file.xml -Destination '\servershare\XML Backups'}

}

[/pre]

Again, thanks for any assistance you can provide as I work through this!

---

<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:** [May 3, 2019, 6:27am UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501/2 "2019-05-03T06:27:59Z")

</div>

How about this?

```
$computers = Get-Content “C:\Computers.txt”

foreach ($computer in $computers) {  
$SourcePath = “\” + $Computer + “\C$\Users\User\AppData\Roaming\FolderName\file.xml”  
$Destination = '\servershare\XML Backups' + $computer  
If (-not (Test-Path -Path $Destination)){  
New-Item -Path $Destination -ItemType Directory -Force  
}  
Copy-Item -Path $SourcePath -Destination $Destination  
}
```
  
Of course you have to have the necessary rights to access all sources and the destination path. 😉

---

<div class="post-metadata">

**Author:** ![pwshliquori](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwshliquori/32/222_2.png) [@pwshliquori](https://forums.powershell.org/u/pwshliquori)\
**Post date:** [May 3, 2019, 7:37am UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501/3 "2019-05-03T07:37:59Z")

</div>

Give this a try, it may take a little longer to process, but will get the computer and user in the destination.

```
$computers = Get-Content "C:\Computers.txt"

foreach ($Computer in $Computers) {
    Invoke-Command -ComputerName $Computer -ScriptBlock {
        foreach ($UserPath in (Get-ChildItem -Path C:\users)) {
            Get-ChildItem -Path "C:\Users\$($UserPath.Name)\AppData\Roaming\" -Filter *.xml |Select-Object -ExpandProperty FullName |
            Foreach-Object {
                $Destination = "\\serverShare\XMLBackups\$using:Computer\$($UserPath.Name)"
                if (-Not (Test-Path -Path $Destination)) {
                    New-Item -Path $Destination -ItemType Directory
                }
                Copy-Item -Path $_ -Destination $Destination
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![smack2k](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@smack2k](https://forums.powershell.org/u/smack2k)\
**Post date:** [May 4, 2019, 9:00pm UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501/4 "2019-05-04T21:00:53Z")

</div>

I will give these a shot and report back…thank you!

---

<div class="post-metadata">

**Author:** ![smack2k](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@smack2k](https://forums.powershell.org/u/smack2k)\
**Post date:** [May 6, 2019, 8:39am UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501/5 "2019-05-06T08:39:34Z")

</div>

[quote quote=155004][/quote]

This worked out well…only issue is its pulling every xml out of each users folder…can I specify the names of two specific xml files in the script so it only grabs those?

---

<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:** [May 6, 2019, 10:58am UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501/6 "2019-05-06T10:58:41Z")

</div>

You could try my approach. 😉 It’s always good to have options.

---

<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:32pm UTC](https://forums.powershell.org/t/copy-file-from-users-appdata-folder-to-server-folder/12501/7 "2024-05-16T20:32:52Z")

</div>

Closed on May 16, 2024, 8:32pm UTC


