Copy file from Users AppData Folder to Server Folder

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!

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. :wink:

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
            }
        }
    }
}

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

[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?

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