MJJM
MJJM

Reputation: 345

Copy-Item and exclude folders

I need to copy all of my c:\inetpub directory to a new location but exclude the following folders and their subfolders:

c:\inetpub\custerr
c:\inetpub\history
c:\inetpub\logs
c:\inetpub\temp
c:\inetpub\wwwroot

So far I am doing this:

# Directory name is created with a format string
$dirName = "\\servername\folder1 _ {0}\inetpub" -f (get-date).ToString("yyyy-MM-dd-hh-mm-ss")
$dirName # Check the output

# Create dir if needed
if(-not (test-path $dirName)) {
    md $dirName | out-null
} else {
    write-host "$dirName already exists!"
}

#Copy Backup File to Dir
Copy-Item "\\servername\c$\inetpub\*" $dirName -recurse

Upvotes: 23

Views: 55983

Answers (5)

satnhak
satnhak

Reputation: 9861

Oh, the answer was SO simple, but it seems we are all PowerShell noobs.

New-Item -ItemType Directory -Force -Path $outDir # directory must exist
Copy-Item $inDir\* $outDir -Exclude @("node_modules",".yarn") -Recurse

It's the \* that makes it work.

PowerShell is awesome, but...

Upvotes: 20

Hanabi
Hanabi

Reputation: 717

I wrote this for daily use and packaged it in the script module, it maintains all the directory structure and supports wildcards:

function Copy-Folder {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String]$FromPath,

        [Parameter(Mandatory)]
        [String]$ToPath,

        [string[]] $Exclude
    )

    if (Test-Path $FromPath -PathType Container) {
        New-Item $ToPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
        Get-ChildItem $FromPath -Force | ForEach-Object {
            # avoid the nested pipeline variable
            $item = $_
            $target_path = Join-Path $ToPath $item.Name
            if (($Exclude | ForEach-Object { $item.Name -like $_ }) -notcontains $true) {
                if (Test-Path $target_path) { Remove-Item $target_path -Recurse -Force }
                Copy-Item $item.FullName $target_path
                Copy-Folder -FromPath $item.FullName $target_path $Exclude
            }
        }
    }
}

Just call the Copy-Folder -FromPath inetpub -ToPath new-inetpub -Exclude custerr,history,logs,temp,wwwroot

The -FromPath and -ToPath can be omitted,

Copy-Folder inetpub new-inetpub -Exclude custerr,history,logs,temp,wwwroot

Upvotes: 6

JohnM
JohnM

Reputation: 125

Copy-Item -Path (Get-Item -Path "$path\*" -Exclude ('Folder1', 'File.cmd', 'File.exe', 'Folder2')).FullName -Destination $destination -Recurse -Force

Replace:

$path by your source folder

('Folder1', 'File.cmd', 'File.exe', 'Folder2') by your specific files/folder to exclude

$destination by your destination folder

Upvotes: 10

Matt
Matt

Reputation: 46710

This is a simple example of something you could do. Build an array of the parent folders that you want to exclude. Since you are accessing them via UNC paths we cannot really use the c:\ path (We can get around this but what I am about to show should be good enough.).

Then use Get-ChildItem to get all the folders in the inetpub directory. Filter out the exclusions using -notin and pass the rest to Copy-Item

$excludes = "custerr","history","logs","temp","wwwroot"
Get-ChildItem "c:\temp\test" -Directory | 
    Where-Object{$_.Name -notin $excludes} | 
    Copy-Item -Destination $dirName -Recurse -Force

You need at least PowerShell 3.0 for this to work.

Upvotes: 16

Alec Justice
Alec Justice

Reputation: 1

You can do something along the lines of:

  ?{$_.fullname -notmatch '\\old\\'}

after you get a hold of all your folders to filter them down.

This example would exclude anything containing "old" in the name. You can do this for directory you wish to exclude.

A full example:

C:\Example*" -include "*.txt -Recurse |
  ?{$_.fullname -notmatch '\\old\\'}|
    % {Copy-Item $_.fullname "C:\Destination\"}

For multiple excludes you can use -And :

C:\Example*" -include "*.txt -Recurse |
  ?{$_.fullname -notmatch '\\old\\' -And $_.fullname -notmatch '\\old2\\'}|
    % {Copy-Item $_.fullname "C:\Destination\"}

Upvotes: 0

Related Questions