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.

Windows for business | Windows Server | User experience | PowerShell
Community Center | Not monitored
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.7K Reputation points MVP Volunteer Moderator
    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 123.7K Reputation points MVP Volunteer Moderator
    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


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.