none
PowerShell: Using -replace not working for result from Get-ChildItem RRS feed

  • Question

  • We have a selection of user folders for which we are trying to copy just one sub folder and it's contents. Every user has a !Scans folder in their folder. We want to copy that folder. I have written a PowerShell script that will perform the copy of the data, but I am having problems when I try to copy the permissions. Specifically, when I get to the contents of the !Scans folder and use recursion to get all subfolders, my script uses a -replace switch to replace the source folder with the destination folder. The problem is that the replace is not happening, so the permissions that are copied end up being applied to the source file instead of the destination file. I've done some testing with basic strings using the -replace switch and it works for those, just not when I am using the replace on a result. Here is my script.

    $SourceFolder='C:\source'
    $DestinationFolder = 'C:\dest'
    
    Get-ChildItem -Path $SourceFolder -Directory | ForEach-Object {
    
        $WorkingDirectory = $_
        
        Copy-Item -Path $SourceFolder\$WorkingDirectory\!Scans -Destination $DestinationFolder\$WorkingDirectory\!Scans -Recurse
        
        # Set ACL of parent folder
        $acl = Get-Acl $SourceFolder\$WorkingDirectory
        Set-Acl -Path $DestinationFolder\$WorkingDirectory -AclObject $acl
    
        # Set ACL of !Scans folder
        $acl = Get-Acl $SourceFolder\$WorkingDirectory\!Scans
        Set-Acl -Path $DestinationFolder\$WorkingDirectory\!Scans -AclObject $acl
    
        # Set ACL for contents of !Scans folder
        Get-ChildItem -Path $SourceFolder\$WorkingDirectory\!Scans -Recurse | ForEach-Object {
            
            $SourceFile = $_.FullName
            $DestinationFile = $_.FullName -replace $SourceFolder,$DestinationFolder
            Write-Host 'Source: '$SourceFile
            Write-Host 'Destination: '$DestinationFile
            $acl = Get-Acl -Path $SourceFile
            Set-Acl -Path $DestinationFile -AclObject $acl
    
        }
    }

    Note: The Write-Host is left in there just for troubleshooting purposes - it shows that the -replace isn't working.

    Also for your reference, here is a sample directory structure that I am trying to copy.

    PS C:\Windows\system32> Get-ChildItem -Recurse -Path C:\source
    
    
        Directory: C:\source
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    d-----       11/23/2015   5:02 PM                user1                                                                                       
    d-----       11/23/2015   5:03 PM                user2                                                                                       
    d-----       11/23/2015   5:03 PM                user3                                                                                       
    
    
        Directory: C:\source\user1
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    d-----       11/23/2015   5:38 PM                !Scans                                                                                      
    -a----        4/15/2015   7:05 PM           8218 AddToSecureWorks.xlsx                                                                       
    -a----        10/9/2014   5:49 PM         640524 Aegis_Padlock_Manual.pdf                                                                    
    -a----        7/15/2015   3:05 PM            333 AmazonSecurityCredentials.csv                                                               
    
    
        Directory: C:\source\user1\!Scans
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    d-----       11/23/2015   5:39 PM                SubFolder                                                                                   
    -a----       11/20/2015  11:27 AM          74114 ActiveMailboxes.txt                                                                         
    -a----       11/20/2015  10:53 AM           7460 ActiveServiceAccounts.txt                                                                   
    -a----       11/20/2015  10:53 AM          45360 ActiveUsers.txt                                                                             
    
    
        Directory: C:\source\user1\!Scans\SubFolder
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    -a----        11/7/2007   7:00 AM          17734 eula.1042.txt                                                                               
    -a----        11/7/2007   7:00 AM          17734 eula.2052.txt                                                                               
    -a----        11/7/2007   7:00 AM          17734 eula.3082.txt                                                                               
    
    
        Directory: C:\source\user2
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    d-----       11/23/2015   5:10 PM                !Scans                                                                                      
    -a----        10/9/2014   5:49 PM         640524 Aegis_Padlock_Manual.pdf                                                                    
    
    
        Directory: C:\source\user2\!Scans
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    -a----        11/7/2007   7:00 AM          10134 eula.1033.txt                                                                               
    -a----        11/7/2007   7:00 AM          17734 eula.1036.txt                                                                               
    -a----        11/7/2007   7:00 AM          17734 eula.1040.txt                                                                               
    -a----        11/7/2007   7:00 AM            118 eula.1041.txt                                                                               
    -a----        11/7/2007   7:00 AM          17734 eula.1042.txt                                                                               
    
    
        Directory: C:\source\user3
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    d-----       11/23/2015   5:10 PM                !Scans                                                                                      
    -a----        7/15/2015   3:05 PM            333 AmazonSecurityCredentials.csv                                                               
    
    
        Directory: C:\source\user3\!Scans
    
    
    Mode                LastWriteTime         Length Name                                                                                        
    ----                -------------         ------ ----                                                                                        
    -a----        1/28/2015   5:32 PM        1418752 CarbonBlackGuide.doc                                                                        
    



    • Edited by dpmoody Monday, November 23, 2015 11:39 PM Changing font of directory output
    Monday, November 23, 2015 11:32 PM

Answers

  • I figured it out.

    Instead of

    $DestinationFile = $_.FullName -Replace $SourceFolder,$DestinationFolder

    It should be

    $DestinationFile = $_.FullName.Replace($SourceFolder,$DestinationFolder)

    • Marked as answer by dpmoody Monday, November 23, 2015 11:47 PM
    Monday, November 23, 2015 11:47 PM

All replies

  • This is what we use RoboCopy for . It will do al of this with little effort.


    \_(ツ)_/

    Monday, November 23, 2015 11:43 PM
  • I tried robocopy first, it doesn't meet my needs. I want to copy just 1 subfolder and all of it's contents. Robocopy will copy the Subfolder in question, but not it's contents.

    C:\>robocopy \source\ \dest\ !Scans* /e /zb

    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows
    -------------------------------------------------------------------------------
      Started : Monday, November 23, 2015 5:11:34 PM
       Source : C:\source\
         Dest : C:\dest\
        Files : !Scans*
      Options : /S /E /DCOPY:DA /COPY:DAT /ZB /R:1000000 /W:30
    ------------------------------------------------------------------------------
                               0    C:\source\
                               0    C:\source\user1\
                               0    C:\source\user1\!Scans\
                               0    C:\source\user2\
                               0    C:\source\user2\!Scans\
                               0    C:\source\user3\
                               0    C:\source\user3\!Scans\
    ------------------------------------------------------------------------------
                   Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :         7         0         7         0         0         0
       Files :         0         0         0         0         0         0
       Bytes :         0         0         0         0         0         0
       Times :   0:00:00   0:00:00                       0:00:00   0:00:00
       Ended : Monday, November 23, 2015 5:11:34 PM

    Monday, November 23, 2015 11:45 PM
  • I figured it out.

    Instead of

    $DestinationFile = $_.FullName -Replace $SourceFolder,$DestinationFolder

    It should be

    $DestinationFile = $_.FullName.Replace($SourceFolder,$DestinationFolder)

    • Marked as answer by dpmoody Monday, November 23, 2015 11:47 PM
    Monday, November 23, 2015 11:47 PM
  • I tried robocopy first, it doesn't meet my needs. I want to copy just 1 subfolder and all of it's contents. Robocopy will copy the Subfolder in question, but not it's contents.

    C:\>robocopy \source\ \dest\ !Scans* /e /zb

    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows
    -------------------------------------------------------------------------------
      Started : Monday, November 23, 2015 5:11:34 PM
       Source : C:\source\
         Dest : C:\dest\
        Files : !Scans*
      Options : /S /E /DCOPY:DA /COPY:DAT /ZB /R:1000000 /W:30
    ------------------------------------------------------------------------------
                               0    C:\source\
                               0    C:\source\user1\
                               0    C:\source\user1\!Scans\
                               0    C:\source\user2\
                               0    C:\source\user2\!Scans\
                               0    C:\source\user3\
                               0    C:\source\user3\!Scans\
    ------------------------------------------------------------------------------
                   Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :         7         0         7         0         0         0
       Files :         0         0         0         0         0         0
       Bytes :         0         0         0         0         0         0
       Times :   0:00:00   0:00:00                       0:00:00   0:00:00
       Ended : Monday, November 23, 2015 5:11:34 PM

    You need to use the right options.  Post in a RoboCopy forum and they will help you understand how to use RoboCopy,  RC is not a PowerShell or script command.


    \_(ツ)_/

    Monday, November 23, 2015 11:52 PM
  • LOL, thanks - I know how to use robocopy. It doesn't meet what I'm trying to do.
    Monday, November 23, 2015 11:53 PM
  • LOL, thanks - I know how to use robocopy. It doesn't meet what I'm trying to do.

    The example you posted is wrong for what you say you are trying to do.

    Don't worry about it if you like what you have.


    \_(ツ)_/

    Tuesday, November 24, 2015 12:16 AM
  • OK, my interest is piqued. How would you do this using RoboCopy?
    Tuesday, November 24, 2015 12:23 AM
  • I don't want to get into a debate.  I suggest that you start by reading all of the options to see how RC includes and excludes things.  Also look at how it decides what folders to create and not create.  It is all there.

    You could also search for any number of good articles on how to use Robocopy to do selected copies.


    \_(ツ)_/

    Tuesday, November 24, 2015 12:26 AM
  • I'm not trying to debate with you either. I'm telling you that I have read all of the different options, I have a lot of experience using RoboCopy, and unfortunately it doesn't meet my needs for what I am trying to do. Since you were insisting that I was wrong, I assumed you had a viable solution. I was mistaken. Sorry.
    Tuesday, November 24, 2015 12:30 AM
  • You are specifying one source and one destination and a bogus file name which will never be found.  YOUneed to specif areal source and a real target.  Why is that a problem.

    You know the sources and destinations.  Why not use them?

    dir c;\source -directory | %{
          Join-Path $_.Fullname '!Scans'
          Join-Path ($_.Fullname -replace 'c:\source','c:\dest' ) '!Scans'
    }

    That is all.  Just use that with RC. All security and subfolders will be copied.


    \_(ツ)_/

    Tuesday, November 24, 2015 12:38 AM
  • The source and destination were real folders. In the results you can see the directory structure and all !Scans folders were copied, just not the contents of the !Scans folder. I could certainly use a combination of scripting and RoboCopy, but if I'm using scripting of some sort already, why not just do it all with scripting? RoboCopy by itself is not enough as I don't know what needs to be excluded, I just know the one sub folder to include, not even the parent folder name.
    Tuesday, November 24, 2015 12:53 AM
  • Don't worry about it.  One day you will see what you are ignoring.


    \_(ツ)_/

    Tuesday, November 24, 2015 12:58 AM
  • Thanks! Don't take yourself so seriously! :)
    • Edited by dpmoody Tuesday, November 24, 2015 1:03 AM
    Tuesday, November 24, 2015 1:03 AM