Powershell script to move files from one folder to another on remote servers

Jose P 21 Reputation points
2023-01-09T13:39:51.67+00:00

Would like to create a powershell script that can move files from one folder to another in multiple servers.

I have multiple servers, server1, server2, server3...
Within each server, there are files currently residing in user desktop named ServerNameFileA.csv, ServerNameFileB.csv, ServerNameFileC.csv, and so on.
Would like to move the files into C:\Save folder within it's respective server.
For example:
Move Server1FileA.csv from user desktop to server1 c:\save.

I made a list of server names, and I tried the following script, but it's not moving anything:

$ServerName = Get-Contect C:\Save\ServerNameList.txt
for each ($Server in $ServerName) {
Copy-Item -Path Users\john.doe\Desktop\S*.csv -Destination c$\Save -FromSession $_
}

"Cannot validate argument or parameter 'FromSession'.
If I take FromSesion part out, nothing happens.

A similar script that I tried was:

Get-Contect C:\Save\ServerNameList.txt | %{
Write-host $_
Copy-Item -Path Users\john.doe\Desktop\S*.csv -Destination c$\Save -FromSession $_
}

Error was different (Cannot convert the "ServerName1" value of type "System.String" to type "System.Management.Automation.Runspaces.PSSession".

Thank you for any help.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-01-09T14:48:18.667+00:00

    You could use some Powershell training. Download and review this document.

    https://www.sapien.com/books_training/Windows-PowerShell-4

    Have you already configured your servers for remoting access? If you can't connect you would need to RDP to each server and run this command.

    winrm quickconfig  
    

    Do you need to do this process for one user or many? This code assumes multiple users.

    $sb = {                     # This is the code that runs on the remote machine   
        "Running on {0}" -f $env:COMPUTERNAME  
        $users = Get-ChildItem 'c:\users' -directory   
        "We found {0} users." -f $users.count    
        foreach ($user in $users) {  
            "Checking user {0}" -f $user.name  
            $DesktopCSV = Get-ChildItem $("{0}\Desktop" -f $user.fullname ) -filter s*.csv  
            "CSV count = {0}" -f $DesktopCSV.count    
            if ($DesktopCSV.count -gt 0) {  
                $DesktopCSV | Copy-Item -Destination C:\Save -whatif     # remove the whatif to actually do the copy  
            }   
        }  
    }  
      
    #$Credential = Get-Credential        # if you wish to be prompted for credentials, do it here.   
      
    #$User = ".\admin"                   # or hardcode the account name +pswd if you need it.   
    #$PWord = ConvertTo-SecureString -String "admin" -AsPlainText -Force  
    #$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord  
      
      
    $ServerName = Get-Content C:\Save\ServerNameList.txt  
    foreach ($Server in $ServerName) {  
        "--------------{0}------------" -f $server   
        # If you are in an AD environment, and you are executing the script as an account that has admin access  
        # to the remote systems, you can use this call  
        Invoke-Command -ComputerName $Server -ScriptBlock $sb   
      
        # If you need to provide credentials to the remote system, then pass the credential value  
        #Invoke-Command -ComputerName $Server -ScriptBlock $sb -Credential $Credential   
    }  
      
    
     
    
    1 person found this answer helpful.

  2. Kostya Chaban 1 Reputation point
    2023-01-09T14:01:04.75+00:00

    Hello. When you try to copy file form remote server, at first you need to do connection/new session.
    Try something like that, I don´t sure that it works, because I don´t have test environment to check now.

    $ServerName = Get-Contect C:\Save\ServerNameList.txt
    for each ($Server in $ServerName) {
    $Session = New-PSSession -ComputerName $Server -Credential "yourdomain\youruser"
    Copy-Item -Path Users\john.doe\Desktop\S*.csv -Destination c$\Save -FromSession $Session -ToSession $Session
    }


  3. Bjoern Peters 8,921 Reputation points
    2023-01-09T18:20:36.197+00:00

    Hi @Jose P

    another edition of a script ;-)
    Change the paths, please...

    $serverlist = get-content c:\temp\serverlist.txt  
    foreach ($server in $serverlist) {  
        $session = New-PSSession -ComputerName $server  
        Invoke-Command -Session $session -ScriptBlock {  
            $source = "\\$server\c$\User\*.csv"  
            $destination = "\\$server\c$\Save\"  
            Move-Item -Path $source -Destination $destination  
        }  
    }  
    

    I hope my answer is helpful to you,

    Your
    Bjoern Peters

    If the reply was helpful, please upvote and/or accept it as an answer, as this helps others in the community with similar questions. Thanks!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.