Friday, April 8, 2016

Using the Copy-Item Cmdlet to exclude files

I recently began switching over some of my command line tasks over to their PowerShell equivalents.

Of course, the Copy-Item cmdlet is the PowerShell replacement to xcopy or RoboCopy.  https://technet.microsoft.com/en-us/library/hh849793.aspx

Of course, the TechNet article provides the available switches and numerous examples, but does not provide a specific example for EXCLUDING FILES!!

When I had previously used robocopy, I had used the /XF switch to exclude files, however, I could not achieve the same result when using this PowerShell command:


$ExclusionFiles = @("packages.config", "connectionStrings.config")

    #Copy over the items
    Copy-Item -Path $SourcePath -Destination $DestinationPath -Exclude $ExclusionFiles -Recurse -Force

Well, as it turns out, my $SourcePath variable needed to append a wildcard character to the end of the path in order for the Exclude switch to properly work as follows:


$ExclusionFiles = @("packages.config", "connectionStrings.config")

    #Copy over the items
    Copy-Item -Path "$SourcePath\*" -Destination $DestinationPath -Exclude $ExclusionFiles -Recurse -Force

That was all that was needed to get my PowerShell script to work in the same way as RoboCopy!!

No comments:

Post a Comment