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:49am

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:04am

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:42am

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 39 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 15 hours 36 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 12:13pm

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:27am

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

This is necessary if you using a wildcard in the $source path
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 3:38am

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

Other recent topics Other recent topics