Compress-Archive, exclude the containing folder
Asked Answered
T

1

7

I need to pack a folder using Compress-Archive in PShell

My folder structure is as following:

|- C:\MyFolder
|--- Files
|------ Start.ps1
|------ Stop.ps1
|--- Manifests
|------ Start.xml
|------ Stop.xml

When I issue this command Compress-Archive -Path "C:\MyFolder" -DestinationPath "C:\MyFolder.zip"

The result is a .zip which has a root folder called "MyFolder" while what I need is a .zip which replicate the tree of MyFolder without having MyFolder as the root one. Is that possible?

Transect answered 15/9, 2021 at 9:32 Comment(0)
M
19

The Path parameter accepts specific file names and file names with wildcards. So instead of C:\MyFolder as Path parameter you could do pass "C:\MyFolder\*"(all its files and subdirectories hence skipping the root directory)

Compress-Archive -Path "C:\MyFolder\*" -DestinationPath "C:\MyFolder.zip"

This is also documented here,

-Path

Specifies the path or paths to the files that you want to add to the archive zipped file. To specify multiple paths, and include files in multiple locations, use commas to separate the paths.

This parameter accepts wildcard characters. Wildcard characters allow you to add all files in a directory to your archive file.

Using wildcards with a root directory affects the archive's contents:

To create an archive that includes the root directory, and all its files and subdirectories, specify the root directory in the Path without wildcards. For example: -Path C:\Reference

To create an archive that excludes the root directory, but zips all its files and subdirectories, use the asterisk (*) wildcard. For example: -Path C:\Reference\*

To create an archive that only zips the files in the root directory, use the star-dot-star (*.*) wildcard. Subdirectories of the root aren't included in the archive. For example: -Path C:\Reference\*.*

Mercia answered 15/9, 2021 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.