@@ -12,7 +12,7 @@ title: Compress-Archive
12
12
# Compress-Archive
13
13
14
14
## SYNOPSIS
15
- Creates an archive, or zipped file, from specified files and folders.
15
+ Creates a compressed archive, or zipped file, from specified files and folders.
16
16
17
17
## SYNTAX
18
18
@@ -60,55 +60,71 @@ Compress-Archive -LiteralPath <String[]> [-DestinationPath] <String> [-Compressi
60
60
61
61
## DESCRIPTION
62
62
63
- The ` Compress-Archive ` cmdlet creates a zipped ( or compressed) archive file from one or more
64
- specified files or folders. An archive file allows multiple files to be packaged, and optionally
65
- compressed, into a single zipped file for easier distribution and storage. An archive file can be
66
- compressed by using the compression algorithm specified by the CompressionLevel parameter.
63
+ The ` Compress-Archive ` cmdlet creates a compressed, or zipped, archive file from one or more
64
+ specified files or folders. An archive packages multiple files, with optional compression, into a
65
+ single zipped file for easier distribution and storage. An archive file can be compressed by using
66
+ the compression algorithm specified by the ** CompressionLevel** parameter.
67
67
68
- The ` Compress-Archive ` cmdlet relies upon the Microsoft .NET API ` System.IO.Compression.ZipArchive `
69
- to compress files. Therefore, the maximum file size that is 2 GB. This is a limitation of the
70
- underlying API.
68
+ The ` Compress-Archive ` cmdlet uses the Microsoft .NET API [ System.IO.Compression.ZipArchive] ( /dotnet/api/system.io.compression.ziparchive )
69
+ to compress files. The maximum file size is 2 GB because there's a limitation of the underlying API.
70
+
71
+ Some examples use splatting to reduce the line length of the code samples. For more information, see
72
+ [ about_Splatting] ( ../Microsoft.PowerShell.Core/About/about_Splatting.md ) .
71
73
72
74
## EXAMPLES
73
75
74
76
### Example 1: Create an archive file
75
77
76
- This command creates a new archive file, ` Draft.zip ` , by compressing two files, ` Draftdoc.docx ` and
78
+ This example creates a new archive file, ` Draft.zip ` , by compressing two files, ` Draftdoc.docx ` and
77
79
` diagram2.vsd ` , specified by the ** Path** parameter. The compression level specified for this
78
- operation is Optimal.
80
+ operation is ** Optimal** .
79
81
80
82
``` powershell
81
- Compress-Archive -Path C:\Reference\Draftdoc.docx, C:\Reference\Images\diagram2.vsd -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip
83
+ $compress = @{
84
+ Path= "C:\Test\Reference\Draftdoc.docx", "C:\Test\Reference\Images\diagram2.vsd"
85
+ CompressionLevel = "Optimal"
86
+ DestinationPath = "C:\Test\Archives\Draft.Zip"
87
+ }
88
+ Compress-Archive @compress
82
89
```
83
90
84
- ### Example 2: Create an archive file ( using LiteralPath)
91
+ ### Example 2: Create an archive file using LiteralPath
85
92
86
- This command creates a new archive file, ` Draft.zip ` , by compressing two files, ` Draft doc.docx ` and
87
- ` Diagram [2].vsd ` , specified by the ** LiteralPath** parameter. The compression level specified for this
88
- operation is Optimal.
93
+ This example creates a new archive file, ` Draft.zip ` , by compressing two files, ` Draft doc.docx ` and
94
+ ` Diagram [2].vsd ` , specified by the ** LiteralPath** parameter. The compression level specified for
95
+ this operation is ** Optimal** .
89
96
90
97
``` powershell
91
- Compress-Archive -LiteralPath 'C:\Reference\Draft Doc.docx', 'C:\Reference\Images\Diagram [2].vsd' -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip
98
+ $compress = @{
99
+ LiteralPath= "C:\Test\Reference\Draft Doc.docx", "C:\Test\Reference\Images\Diagram [2].vsd"
100
+ CompressionLevel = "Optimal"
101
+ DestinationPath = "C:\Test\Archives\Draft.Zip"
102
+ }
103
+ Compress-Archive @compress
92
104
```
93
105
94
106
### Example 3: Create an archive with wildcard characters
95
107
96
- This command creates a new archive file, ` Draft.zip ` , in the ` C:\Archives ` folder. The new archive
97
- file contains every file in the ` C:\Reference ` folder, because a wildcard character was used in
98
- place of specific file names in the ** Path** parameter.
108
+ This example creates a new archive file, ` Draft.zip ` , in the ` C:\Archives ` folder. The new archive
109
+ file contains every file, folder, and subfolder in the ` C:\Reference ` root folder, because an
110
+ asterisk (` * ` ) wildcard character was used in place of specific file names in the ** Path**
111
+ parameter. The root folder, ` Reference ` , is excluded from the archive.
99
112
100
113
``` powershell
101
114
Compress-Archive -Path C:\Reference\* -CompressionLevel Fastest -DestinationPath C:\Archives\Draft
102
115
```
103
116
104
- Notice that the file name extension .zip was not added to the value of the ** DestinationPath**
105
- parameter. PowerShell appends the .zip extension to the file name automatically. The specified
106
- compression level is ** Fastest** , which might result in a larger output file, but compresses a large
107
- number of files faster.
117
+ Notice that the file name extension ` .zip ` wasn't added to the value of the ** DestinationPath**
118
+ parameter. PowerShell appends the ` .zip ` extension to the file name automatically. The specified
119
+ compression level is ** Fastest** . The faster compression level will compress large numbers of files
120
+ faster, but might result in a larger output file.
121
+
122
+ > [ !NOTE]
123
+ > To zip only the files in the specified root folder, use the ** star-dot-star** (` *.* ` ) wildcard.
108
124
109
125
### Example 4: Update an existing archive file
110
126
111
- This command updates an existing archive file, Draft.Zip, in the C:\Archives folder.
127
+ This example updates an existing archive file, ` Draft.Zip ` , in the ` C:\Archives ` folder.
112
128
113
129
``` powershell
114
130
Compress-Archive -Path C:\Reference\* -Update -DestinationPath C:\Archives\Draft.Zip
@@ -120,29 +136,31 @@ C:\Reference folder, and also to add new files that have been added to `C:\Refer
120
136
121
137
### Example 5: Create an archive from an entire folder
122
138
123
- This command creates an archive from an entire folder, C:\Reference.
139
+ This example creates an archive from an entire folder, ` C:\Reference ` . The archive contains the root
140
+ folder, ` Reference ` , and all its files, folders, and subfolders.
124
141
125
142
``` powershell
126
143
Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft
127
144
```
128
145
129
- Notice that the file name extension .zip was not added to the value of the ** DestinationPath**
130
- parameter. PowerShell appends the .zip extension to the file name automatically.
146
+ Notice that the file name extension ` .zip ` wasn't added to the value of the ** DestinationPath**
147
+ parameter. PowerShell appends the ` .zip ` extension to the file name automatically.
131
148
132
149
## PARAMETERS
133
150
134
151
### -CompressionLevel
135
152
136
- Specifies how much compression to apply when you are creating the archive file. Faster compression
137
- requires less time to create the file, but can result in larger file sizes. The acceptable values
138
- for this parameter are:
153
+ Specifies how much compression to apply when you're creating the archive file. Faster compression
154
+ requires less time to create the file, but can result in larger file sizes.
139
155
140
- - ** Fastest** . Use the fastest compression method available to decrease processing time; this can result
141
- in larger file sizes.
142
- - ** NoCompression** . Do not compress the source files.
143
- - ** Optimal** . Processing time is dependent on file size.
156
+ If this parameter isn't specified, the command uses the default value, ** Optimal** .
144
157
145
- If this parameter is not specified, the command uses the default value, Optimal.
158
+ The acceptable values for this parameter are as follows:
159
+
160
+ - ** Fastest** . Use the fastest compression method available to reduce processing time. Faster
161
+ compression can result in larger file sizes.
162
+ - ** NoCompression** . Doesn't compress the source files.
163
+ - ** Optimal** . Processing time is dependent on file size.
146
164
147
165
``` yaml
148
166
Type : String
@@ -157,12 +175,28 @@ Accept pipeline input: False
157
175
Accept wildcard characters : False
158
176
` ` `
159
177
178
+ ### -Confirm
179
+
180
+ Prompts you for confirmation before running the cmdlet.
181
+
182
+ ` ` ` yaml
183
+ Type : SwitchParameter
184
+ Parameter Sets : (All)
185
+ Aliases : cf
186
+
187
+ Required : False
188
+ Position : Named
189
+ Default value : False
190
+ Accept pipeline input : False
191
+ Accept wildcard characters : False
192
+ ` ` `
193
+
160
194
### -DestinationPath
161
195
162
196
Specifies the path to the archive output file. This parameter is required. The specified
163
- **DestinationPath** value should include the desired name of the output zipped file; it specifies
197
+ **DestinationPath** value should include the desired name of the output zipped file, and specifies
164
198
either the absolute or relative path to the zipped file. If the file name specified in
165
- **DestinationPath** does not have a .zip file name extension, the cmdlet adds a .zip file name
199
+ **DestinationPath** doesn't have a ` .zip` file name extension, the cmdlet adds a ` .zip` file name
166
200
extension.
167
201
168
202
` ` ` yaml
@@ -196,7 +230,7 @@ Accept wildcard characters: False
196
230
# ## -LiteralPath
197
231
198
232
Specifies the path or paths to the files that you want to add to the archive zipped file. Unlike the
199
- **Path** parameter, the value of **LiteralPath** is used exactly as it is typed. No characters are
233
+ **Path** parameter, the value of **LiteralPath** is used exactly as it's typed. No characters are
200
234
interpreted as wildcards. If the path includes escape characters, enclose each escape character in
201
235
single quotation marks, to instruct PowerShell not to interpret any characters as escape sequences.
202
236
To specify multiple paths, and include files in multiple locations in your output zipped file, use
@@ -221,6 +255,19 @@ parameter can accept wildcard characters. Wildcard characters allow you to add a
221
255
to your zipped archive file. To specify multiple paths, and include files in multiple locations in
222
256
your output zipped file, use commas to separate the paths.
223
257
258
+ To zip all the files, folders, and subfolders in a specified root folder, use the asterisk (`*`)
259
+ wildcard. The root folder is excluded from the archive.
260
+
261
+ For example :
262
+
263
+ ` -Path C:\R eference\* `
264
+
265
+ To zip only the files in the specified root folder, use the **star-dot-star** (`*.*`) wildcard.
266
+
267
+ For example :
268
+
269
+ ` -Path C:\R eference\* .*`
270
+
224
271
` ` ` yaml
225
272
Type: String[]
226
273
Parameter Sets: Path, PathWithUpdate, PathWithForce
@@ -235,9 +282,9 @@ Accept wildcard characters: True
235
282
236
283
# ## -Update
237
284
238
- Updates the specified archive by replacing older versions of files in the archive with newer
239
- versions of files that have the same names. You can also add this parameter to add files to an
240
- existing archive.
285
+ Updates the specified archive by replacing older file versions in the archive with newer file
286
+ versions that have the same names. You can also add this parameter to add files to an existing
287
+ archive.
241
288
242
289
` ` ` yaml
243
290
Type: SwitchParameter
@@ -251,25 +298,9 @@ Accept pipeline input: False
251
298
Accept wildcard characters: False
252
299
` ` `
253
300
254
- ### -Confirm
255
-
256
- Prompts you for confirmation before running the cmdlet.
257
-
258
- ` ` ` yaml
259
- Type : SwitchParameter
260
- Parameter Sets : (All)
261
- Aliases : cf
262
-
263
- Required : False
264
- Position : Named
265
- Default value : False
266
- Accept pipeline input : False
267
- Accept wildcard characters : False
268
- ` ` `
269
-
270
301
# ## -WhatIf
271
302
272
- Shows what would happen if the cmdlet runs. The cmdlet is not run.
303
+ Shows what would happen if the cmdlet runs. The cmdlet isn't run.
273
304
274
305
` ` ` yaml
275
306
Type: SwitchParameter
@@ -287,8 +318,7 @@ Accept wildcard characters: False
287
318
288
319
This cmdlet supports the common parameters : -Debug, -ErrorAction, -ErrorVariable,
289
320
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
290
- -WarningAction, and -WarningVariable. For more information, see
291
- [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
321
+ -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
292
322
293
323
# # INPUTS
294
324
@@ -304,5 +334,4 @@ You can pipe a string that contains a path to one or more files.
304
334
305
335
# # RELATED LINKS
306
336
307
- [Expand-Archive](expand-archive.md)
308
-
337
+ [Expand-Archive](Expand-Archive.md)
0 commit comments