Copy-Item and exclude one folder

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 can someone help please?

# 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

August 19th, 2015 9:50am

Make your life easy and just use robocopy.

http://ss64.com/nt/robocopy.html

If you really want to pursue PowerShell, use Get-ChildItem to get your list of folders, pipe through Where-Object to exclude your specific folders, and then use Copy-Item.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 9:57am

Thing is I am using powershell to create a new folder each night. I wonder if I could do that and then call robocopy in the PS script to make the copy and exclude the directories?
August 19th, 2015 10:05am

Thing is I am using powershell to create a new folder each night. I wonder if I could do that and then call robocopy in the PS script to make the copy and exclude the directories?

Yes, you could.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 10:08am

Hello,

I think following would work:

Get-ChildItem "\\servername\c$\inetpub\*"  -Recurse -Exclude "exclude_*" | Copy-Item -Destination $dirName

No.

August 19th, 2015 10:43am

Try this:
gci $source -Recurse | Where-Object {($_.FullName -notlike "c:\inetpub\custerr*") -and ($_.FullName -notlike "c:\inetpub\history*") -and ($_.FullName -notlike "c:\inetpub\logs*")-and ($_.FullName -notlike "c:\inetpub\temp*")-and ($_.FullName -notlike "c:\inetpub\wwwroot*")}  | Copy-Item -destination $dirName

  
  • Edited by KranzerM 15 hours 1 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 10:48am

Thanks Kranzer, it wkind of works by copying files but they aren't retained in the folders I was expecting.  For example inetpub has lots of files in it where i would expect several website folder and further then their contents copied too.  It looks to be copying everything at the root folder level and then I also get the error below.

Copy-Item : Item with specified name \\servername\foldername\inetpub\xxfoldername already exists.

August 19th, 2015 11:18am

try this :)

$source = "\\servername\c$\inetpub\*"

gci $source -Recurse | Where-Object {($_.FullName -notlike "c:\inetpub\custerr*") -and ($_.FullName -notlike "c:\inetpub\history*") -and ($_.FullName -notlike "c:\inetpub\logs*")-and ($_.FullName -notlike "c:\inetpub\temp*")-and ($_.FullName -notlike "c:\inetpub\wwwroot*")}   | Copy-Item -Destination {Join-Path $dirName $_.FullName.Substring($source.length)}



  • Edited by KranzerM 14 hours 59 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 12:13pm

Try this:
gci $source -Recurse | Where-Object {($_.FullName -notlike "c:\inetpub\custerr*") -and ($_.FullName -notlike "c:\inetpub\history*") -and ($_.FullName -notlike "c:\inetpub\logs*")-and ($_.FullName -notlike "c:\inetpub\temp*")-and ($_.FullName -notlike "c:\inetpub\wwwroot*")}  | Copy-Item -destination $dirName

  
  • Edited by KranzerM Wednesday, August 19, 2015 4:09 PM
August 19th, 2015 2:45pm

try this :)

$source = "\\servername\c$\inetpub\*"

gci $source -Recurse | Where-Object {($_.FullName -notlike "c:\inetpub\custerr*") -and ($_.FullName -notlike "c:\inetpub\history*") -and ($_.FullName -notlike "c:\inetpub\logs*")-and ($_.FullName -notlike "c:\inetpub\temp*")-and ($_.FullName -notlike "c:\inetpub\wwwroot*")}   | Copy-Item -Destination {Join-Path $dirName $_.FullName.Substring($source.length)}



  • Edited by KranzerM Wednesday, August 19, 2015 4:11 PM
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 4:10pm

That copies folders but each folder inside of inetpub has the first character taken away from it?  I have a folder called activedirectory but once copied is called ctivedirectory
August 20th, 2015 3:26am

Replace ($source.length) with ($source.length-1)

This is necessary if you using a wildcard in the $source path
  • Edited by KranzerM 23 hours 32 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 3:37am

That has brought the 1st character back.

It's still copying the folders and their contents that we've told it to exclude?  I have tried removing the * from the excludes but it has no effect thanks

August 20th, 2015 4:08am

Also trying this doesn't work $_.Name -notlike "custerr"
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:14am

Check this link - http://techblog.dorogin.com/2013/01/powershell-recursively-copy-folder-structure.html
August 20th, 2015 4:27am

You have to exclude like this:
$_.FullName -notlike "\\servername\c$\inetpub\custerr*"
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:42am

I tried that already which didn't exclude anything so tried Name instead.  Have you tested the script yourself and verified it works?
August 20th, 2015 4:47am

Make your life easy and just use robocopy.

http://ss64.com/nt/robocopy.html

If you really want to pursue PowerShell, use Get-ChildItem to get your list of folders, pipe through Where-Object to exclude your specific folders, and then use Copy-Item.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:59am

yeah for me its working. 
maybe we overlook something. 

Please try the following and tell me if your output is without the excluded folders.
gci $source  | Where-Object {($_.FullName -notlike "\\servername\c$\inetpub\custerr*") -and ($_.FullName -notlike "\\servername\c$\inetpub\history*") -and ($_.FullName -notlike "\\servername\c$\inetpub\logs*")-and ($_.FullName -notlike "\\servername\c$\inetpub\temp*")-and ($_.FullName -notlike "\\servername\c$\inetpub\wwwroot*")}   
  • Marked as answer by MJ2012 21 hours 50 minutes ago
August 20th, 2015 5:01am

You have to Copy-Item -Destination $dirName 
  • Marked as answer by MJ2012 21 hours 50 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 5:03am

With a bit of tweaking it's now working thanks for your help

$source = "\\server\c$\inetpub\*"
gci $source  | Where-Object {($_.FullName -notlike "\\server\c$\inetpub\custerr*") -and ($_.FullName -notlike "\\server\c$\inetpub\logs*") } | Copy-Item -Destination $dirName -Recurse

August 20th, 2015 5:23am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics