powershell - Copy-Item and exclude folders -


i need copy of c:\inetpub directory new location exclude following folders , subfolders:

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

so far doing this:

# directory name created format string $dirname = "\\servername\folder1 _ {0}\inetpub" -f (get-date).tostring("yyyy-mm-dd-hh-mm-ss") $dirname # check output  # create dir if needed if(-not (test-path $dirname)) {     md $dirname | out-null } else {     write-host "$dirname exists!" }  #copy backup file dir copy-item "\\servername\c$\inetpub\*" $dirname -recurse 

this simple example of do. build array of parent folders want exclude. since accessing them via unc paths cannot use c:\ path (we can around show should enough.).

then use get-childitem folders in inetpub directory. filter out exclusions using -notin , pass rest 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 @ least powershell 3.0 work.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -