Personally, I would separate the paths to exclude and the extensions to exclude for this to make things a lot more redable.
Using the -Exclude
parameter on Get-ChildItem it is easy enough to use an array of extensions where wildcards are permitted.
Mind you, This parameter can only be used if you also add the -Recurse
switch or let the path end in \*
for the Get-ChildItem cmdlet.
This will also help in reducing the number of items listed. (i.e. by excluding *.vb
, you are automatically also excluding AssemblyInfo.vb
)
The paths can be combined in a regex string. (for details, see below)
By doing so, the code could look like this:
$ExtensionsToExclude = '*.vb', '*.cs', '*.pdb', '*.sln', '*.*proj*'
$PathsToExclude = '\\(obj|Debug|Release|Properties|My Project)\\|\\Web\.(Debug|Release)\.config\\'
Get-ChildItem -Path <path> -Recurse -Exclude $ExtensionsToExclude |
Where-Object { $_.FullName -notmatch $PathsToExclude } |
Compress-Archive -DestinationPath <destination> -Update
Regex details:
Match either the regular expression below (attempting the next alternative only if this one fails)
\\ Match the character “\” literally
( Match the regular expression below and capture its match into backreference number 1
Match either the regular expression below (attempting the next alternative only if this one fails)
obj Match the characters “obj” literally
| Or match regular expression number 2 below (attempting the next alternative only if this one fails)
Debug Match the characters “Debug” literally
| Or match regular expression number 3 below (attempting the next alternative only if this one fails)
Release Match the characters “Release” literally
| Or match regular expression number 4 below (attempting the next alternative only if this one fails)
Properties Match the characters “Properties” literally
| Or match regular expression number 5 below (the entire group fails if this one fails to match)
My\ Project Match the characters “My Project” literally
)
\\ Match the character “\” literally
| Or match regular expression number 2 below (the entire match attempt fails if this one fails to match)
\\ Match the character “\” literally
Web Match the characters “Web” literally
\. Match the character “.” literally
( Match the regular expression below and capture its match into backreference number 2
Match either the regular expression below (attempting the next alternative only if this one fails)
Debug Match the characters “Debug” literally
| Or match regular expression number 2 below (the entire group fails if this one fails to match)
Release Match the characters “Release” literally
)
\. Match the character “.” literally
config Match the characters “config” literally
\\ Match the character “\” literally
Hope that helps