Copy file to multiple computers | Permission should not change

Santhosh B S 106 Reputation points
2023-07-05T06:57:35.3633333+00:00

Team,

I have custom made run.bat file and certain permission set on that file. I need to copy that file to multiple domain joined devices. I have a script which is copying the file to destination folder but permission is getting changed.

I need a script which needs to copy the file and also permission should not change post copying.

Kindly help.

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

2 answers

Sort by: Most helpful
  1. Matt-Radcliffe 10 Reputation points
    2023-12-19T09:33:16.5633333+00:00

    This task can be done via copy tools easily and effectively such as Teracopy , Gs Richcopy 360 and Freefilesync , all can help to copy files from source to destination with preserving shared/NTFS permissions .

    Also you can check the below PowerShell script for handling this issue

    $sourceFile = "C:\path\to\run.bat"
    $destinationFolder = "\\computer1\share\folder"
    $computers = @("computer1", "computer2", "computer3")
    
    foreach ($computer in $computers) {
        $destinationPath = "\\$computer\$destinationFolder"
    
        # Copy the file to the destination folder
        Copy-Item -Path $sourceFile -Destination $destinationPath -Force
    
        # Get the source file's ACL
        $acl = Get-Acl -Path $sourceFile
    
        # Set the destination file's ACL to match the source file's ACL
        $destinationFile = Join-Path -Path $destinationPath -ChildPath (Split-Path -Leaf $sourceFile)
        Set-Acl -Path $destinationFile -AclObject $acl
    }
    

    Note :

    • $sourceFile: The path to the run.bat file on your local machine.
    • $destinationFolder: The shared folder path on the remote computers where you want to copy the file.
    • $computers: An array of the names of the domain joined computers where you want to copy the file.

    I hope these info help

    1 person found this answer helpful.
    0 comments No comments

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2023-07-05T08:08:26.44+00:00

    Hi @Santhosh Basavarajappa ,

    maybe Robocopy is a tool to get your requirement done. With robocopy it's possible to copy the ACL as well:

    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy


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

    Regards

    Andreas Baumgarten

    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.