Converting the FileSystemObject's CopyFolder Method

Definition: Copies one or more folders to a specified location.

CopyFolder

The CopyFolder method recursively copies all files, subfolders, files within the subfolders, and so on from the source folder to the destination folder. PowerShell uses the Copy-Item cmdlet to copy folders, but it works a little differently. Here are some examples.

This command copies the C:\temp folder and all its contents to the C:\old folder:

Copy-Item c:\temp c:\old

However, if the C:\temp folder contains subfolders, the above command simply creates a new folder under C:\old named temp, with none of the contents copied. To recursively copy everything from C:\temp to C:\old, you need to use a wildcard, like this:

Copy-Item c:\temp\* c:\old

C:\old must exist for the preceding command to work. If C:\old does not exist, the preceding command will create it and copy all the files from C:\temp to the new C:\old folder, but none of the subfolders within C:\temp will be copied.

Yes, we know, it’s a little confusing. Play around with it, copying folders with and without subfolders. We’d say you’ll get the hang of it, but we’re still a little confused ourselves.

See conversions of other FileSystemObject methods and properties.
Return to the VBScript to Windows PowerShell home page