Script to copy a file to one folder but if this one exists, don't replace it

WinWorker 61 Reputation points
2020-11-12T14:21:27.013+00:00

Hi all,

I need a script (or command, whatever) to copy a file (for example, abc.txt file) from a shared folder to all computers (in c:\temp directory) in my environment. However, in case of existing this file in a few computers (in c:\temp), just skip it.

Thank you very much.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 107.9K Reputation points MVP
    2020-11-12T15:25:17.367+00:00
    $sourcefolder = "c:\1"
    $targetFolder = "c:\2"
    $filename = "1.txt"
    
    $targetfile = Test-Path  -Path "$targetFolder\$filename"
    if ($targetfile -eq $false)
    {
    Write-Output "Here we go"
    Copy-Item -Path $sourcefolder\$filename -Destination $targetFolder
    }
    else{
    Write-Output "File aready exist in targetfolder"
    } 
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Bill Stewart 181 Reputation points
    2020-11-12T18:12:19.607+00:00

    My suggestion would be to use Group Policy rather than trying to script it.

    1 person found this answer helpful.
    0 comments No comments

  2. Andreas Baumgarten 107.9K Reputation points MVP
    2020-11-12T18:17:32.363+00:00

    @WinWorker

    The script I posted above will run in silent mode without any problem. The Write-Output is just to see what happens in the "interactive user mode".

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

  3. WinWorker 61 Reputation points
    2020-11-12T17:41:10.337+00:00

    @Andreas Baumgarten ,

    It helps me a lot. However, I need to execute this script in silent mode, in such a way that a user doesn't view the process.

    Thank you

    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.