Copy file to a path and leave message on log

Rafael Aguilar 496 Reputation points
2022-03-25T20:45:22.913+00:00

HiTeam.

I'm not very powershell savvy but I need some help or guidance to do the following:

I need to copy 2 files from a network path to a path on the computer.

File 1 to this path: C:\Windows\PolicyDefinitions if the file exists don't copy it but leave a message in a .log or .txt log, if it doesn't exist copy it.

File 2 to this path: C:\Windows\PolicyDefinitions\en-US, if the file exists, do not copy it but leave a message in a .log or .txt file, if it does not exist, copy it.

Where can I start?

Thanks for your help.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,797 questions
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,463 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 104K Reputation points MVP
    2022-03-26T02:38:47.79+00:00

    Hi @Rafael Aguilar ,

    maybe this helps:

    $filename = "filename.txt"  
    $sourcefolder = "\\networkpath\share"  
    $targetfolder = "C:\Windows\PolicyDefinitions"  
    $logfile = "c:\junk\copyfilelog.txt"  
    if (Test-Path -Path "$targetfolder\$filename" -PathType Leaf) {  
      Write-Output "File $filename exists in folder $targetfolder"  
      Out-File -FilePath $logfile -Encoding utf8 -Append -InputObject "File exists in folder"  
    }  
    else {  
      Copy-Item -Path "$sourcefolder\$filename" -Destination $targetfolder  
      Write-Output "File copied to folder"  
    }  
    

    ----------

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

    Regards
    Andreas Baumgarten


  2. Andreas Baumgarten 104K Reputation points MVP
    2022-03-29T18:20:25.867+00:00

    Hi @Rafael Aguilar ,

    maybe this works for you with date and time:

    $filename = "filename.txt"  
    $sourcefolder = "\\networkpath\share"  
    $targetfolder = "C:\Windows\PolicyDefinitions"  
    $logfile = "c:\junk\copyfilelog.txt"  
    $currentDateTime = Get-Date -Format "MM/dd/yyyy HH:mm:ss"  
    if (Test-Path -Path "$targetfolder\$filename" -PathType Leaf) {  
      Write-Output "File $filename exists in folder $targetfolder  - $currentDateTime"  
      Out-File -FilePath $logfile -Encoding utf8 -Append -InputObject "$currentDateTime | File $filename exists in folder $targetfolder"  
    }  
    else {  
      Copy-Item -Path "$sourcefolder\$filename" -Destination $targetfolder  
      Write-Output "File copied to folder"  
    }  
    

    ----------

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

    Regards
    Andreas Baumgarten