What is the meaning of the Powershell Copy-Item -container argument?

I am writing a script for MS PowerShell. This script uses the Copy-Item command. One of the optional arguments to this command is the "container". The documentation for the argument states that specifying this argument "Saves container objects during the copy operation."

This is all good and good, because I would be the last person who would like to have unreserved container objects during the copy operation. But in all seriousness, what does this argument make? In particular, when I copy a disk directory tree from one place to another, what difference does this make with the behavior of the Copy-Item command?

+20
powershell copy-item
Sep 24 '08 at 18:53
source share
2 answers

The container referred to in the documentation is the folder structure. If you are making a recursive copy and want to keep the folder structure, you should use the -container switch. (Note: by default, the -container switch is true, so you won’t need to specify it. If you want to disable it, you can use -container: $false .)

There is a trick for this ... if you make a list of directories and forward it to Copy-Item, it will not preserve the folder structure. If you want to keep the folder structure, you must specify the -path property and the -recurse switch.

+20
Sep 24 '08 at 19:38
source share

I also found the documentation less useful. I did some tests to see how the -Container works with -Recurse when copying files and folders.

Note that -Container means -Container: $true .

This is the file structure that I used for examples:

 # X:. # β”œβ”€β”€β”€destination # └───source # β”‚ source.1.txt # β”‚ source.2.txt # β”‚ # └───source.1 # source.1.1.txt 
  • For all examples, the current location (pwd) is X:\ .
  • I used PowerShell 4.0.



1) To copy only the source folder (empty folder):

 Copy-Item -Path source -Destination .\destination Copy-Item -Path source -Destination .\destination -Container # X:. # β”œβ”€β”€β”€destination # β”‚ └───source # └───source (...) 

The following is the error:

 Copy-Item -Path source -Destination .\destination -Container: $false # Exception: Container cannot be copied to another container. # The -Recurse or -Container parameter is not specified. 

2) To copy the entire structure of folders with files:

 Copy-Item -Path source -Destination .\destination -Recurse Copy-Item -Path source -Destination .\destination -Recurse -Container # X:. # β”œβ”€β”€β”€destination # β”‚ └───source # β”‚ β”‚ source.1.txt # β”‚ β”‚ source.2.txt # β”‚ β”‚ # β”‚ └───source.1 # β”‚ source.1.1.txt # └───source (...) 

3) To copy all descendants (files and folders) into one folder:

 Copy-Item -Path source -Destination .\destination -Recurse -Container: $false # X:. # β”œβ”€β”€β”€destination # β”‚ β”‚ source.1.1.txt # β”‚ β”‚ source.1.txt # β”‚ β”‚ source.2.txt # β”‚ β”‚ # β”‚ └───source.1 # └───source (...) 
+30
Feb 15 '14 at 14:13
source share



All Articles