{"id":1667,"date":"2011-10-03T15:41:26","date_gmt":"2011-10-03T19:41:26","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=1667"},"modified":"2011-10-03T15:41:26","modified_gmt":"2011-10-03T19:41:26","slug":"compress-files-by-extension","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/","title":{"rendered":"Compress Files By Extension"},"content":{"rendered":"<p>I never seem to build my test virtual machines with enough disk space. Which means at some point I start dealing with space issues and resort to all sorts of hacks to buy myself a little time. One thing I do is look for files that I can compact using NTFS compression. For examples text files and most Office files compress pretty well. Thus the challenge is to find those files and compress them.<!--more--><\/p>\n<p>The WMI CIM_DATAFILE class has a Compress() method (as well as Uncompress()). So all I have to do is construct a query to find the appropriate files and then invoke the Compress() method for each result. I have found in the past that when querying CIM_DATAFILE to be as specific as possible, taking special care to filter on the drive. The resulting query can get a tad complicated.<\/p>\n<p>[cc lang=\"PowerSHell\"]<br \/>\nSelect * from CIM_DATAFILE where Drive='c:' AND Path Like '%\\\\work\\\\%'AND Compressed='FALSE' AND FileSize >= 4096 AND (Extension='txt' OR Extension='doc' OR Extension='xml')<br \/>\n[\/cc]<\/p>\n<p>This query will search for all TXT, DOC and XML files anywhere under C:\\Work that are over 4KB in size. I have found that compressing files smaller than 4KB is counter-productive. This query will take a while to run. It would make a nice background job.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\nget-wmiobject CIM_DATAFILE -filter \"Drive='c:' AND Path Like '%\\\\work\\\\%'AND Compressed='FALSE' AND FileSize >= 4096 AND (Extension='txt' OR Extension='doc' OR Extension='xml')\" -asjob<br \/>\n[\/cc]<\/p>\n<p>When the job is complete, the results can be piped to Invoke-WMIMethod. The benefit is that the cmdlet supports -Whatif.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n$files=receive-job 1 -keep<br \/>\n$files | invoke-wmimethod -name Compress -whatif<br \/>\n[\/cc]<\/p>\n<p>Because constructing the query is the most complicated part, I put together a script to do all the hard work for me.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n#requires -version 2.0<\/p>\n<p>[cmdletbinding(SupportsShouldProcess=$True)]<\/p>\n<p>Param (<br \/>\n[Parameter(Mandatory=$True,HelpMessage=\"Enter a file extension, without the period\")]<br \/>\n[ValidateNotNullorEmpty()]<br \/>\n[string[]]$Extension,<br \/>\n[ValidatePattern(\"^[a-zA-Z]:\")]<br \/>\n[string]$Path=\"c:\\\",<br \/>\n[ValidateScript({$_ -ge 4KB})]<br \/>\n[int]$Size=4KB,<br \/>\n[ValidateNotNullorEmpty()]<br \/>\n[string]$Computername=$env:computername,<br \/>\n[switch]$Uncompress<br \/>\n)<\/p>\n<p>Write-Verbose \"Starting $($myinvocation.mycommand)\"<\/p>\n<p>#get first two characters for drive<br \/>\n$Drive=$Path.Substring(0,2)<\/p>\n<p>Write-Verbose \"Connecting to drive $drive on $computername\"<br \/>\nWrite-Verbose \"Finding files >= than $size\"<\/p>\n<p>#build query<br \/>\n$extensions=$extension -as [string]<br \/>\n#parse the extensions and build a complex OR structure for each one<br \/>\n$extFilter=\"(Extension='{0}{1}\" -f ($extensions.replace(\" \",\"' OR Extension='\")),\"')\"<\/p>\n<p>if ($Uncompress) {<br \/>\n    Write-Verbose \"Uncompressing files\"<br \/>\n    $Compress=\"TRUE\"<br \/>\n}<br \/>\nelse {<br \/>\n    $Compress=\"FALSE\"<br \/>\n}<\/p>\n<p>#split off the folder portion of the path, change \\ to \\\\<br \/>\n$folder=$Path.Split(\":\")[1].Replace(\"\\\",\"\\\\\")<\/p>\n<p>$filter=\"Drive='$Drive' AND Path Like '%$Folder\\\\%'AND Compressed='$Compress' AND FileSize >= $size AND $extFilter\"<br \/>\n#define a subset of properties to return in hopes of tweaking performance<br \/>\n$properties=\"Drive,Path,Compressed,FileSize,Extension,CSName,Name\"<\/p>\n<p>Write-Verbose \"Query = $filter\"<\/p>\n<p>Write-Progress -Activity \"Compress File Types\" -status \"Running filter $filter on $computername\" `<br \/>\n-currentoperation \"Searching $Path. Please wait...this may take several minutes...\"<\/p>\n<p>#capture time before the WMI Query<br \/>\n$start=Get-Date<\/p>\n<p>Try {<br \/>\n    Write-Verbose \"Getting files from $Path\"<br \/>\n    $files=Get-WmiObject -class CIM_DATAFILE -filter $filter -Property $properties -computername $computername -ErrorAction Stop<br \/>\n}<br \/>\nCatch {<br \/>\n    Write-Error (\"Failed to run query on $computername. Exception: {0}\" -f $_.Exception.Message)<br \/>\n}<\/p>\n<p>$filecount=($files | measure-object).count<br \/>\nWrite-Verbose \"Found $filecount files\"<\/p>\n<p>#define some variables for Write-Progress<br \/>\nif ($Uncompress) {<br \/>\n    $activity=\"Uncompress File Types\"<br \/>\n    $status=\"Uncompressing\"<br \/>\n }<br \/>\nelse {<br \/>\n  $activity=\"Compress File Types\"<br \/>\n  $status=\"Compressing\"<br \/>\n}<\/p>\n<p>#only process if files were returned<br \/>\nif ($filecount -gt 0) {<br \/>\n    #keep a running count of compressed files<br \/>\n    $iCompressed=0<br \/>\n    $iFailures=0<br \/>\n    $i=0<br \/>\n    Write-Verbose \"Processing files\"<\/p>\n<p>    foreach ($file in $files) {<br \/>\n     $i++<br \/>\n     [int]$percent=($i\/$filecount)*100<br \/>\n     Write-Verbose $file.name<\/p>\n<p>     Write-Progress -Activity $activity -Status $status -currentoperation $file.name -percentComplete $percent<br \/>\n     if ($pscmdlet.ShouldProcess($file.name)) {<br \/>\n       if ($Uncompress) {<br \/>\n        $rc=$file.Uncompress()<br \/>\n       }<br \/>\n       else {<br \/>\n        $rc=$file.Compress()<br \/>\n       }<br \/>\n            Switch ($rc.returnvalue) {<br \/>\n                 0 { $Result=\"The request was successful\"<br \/>\n                     #increment the counter<br \/>\n                     $iCompressed++<br \/>\n                     #write the WMI file object to the pipeline<br \/>\n                     #for later processing<br \/>\n                     write $file | select @{Name=\"Computername\";Expression={$_.CSName}},<br \/>\n                     Name,FileSize,Extension      <\/p>\n<p>                     Break<br \/>\n                   }<\/p>\n<p>                 2 { $Result=\"Access was denied.\" ; Break}<br \/>\n                 8 { $Result=\"An unspecified failure occurred.\"; Break}<br \/>\n                 9 { $Result=\"The name specified was invalid.\"; Break}<br \/>\n                 10 { $Result=\"The object specified already exists.\"; Break}<br \/>\n                 11 { $Result=\"The file system is not NTFS.\"; Break}<br \/>\n                 12 { $Result=\"The operating system is not supported.\"; Break}<br \/>\n                 13 { $Result=\"The drive is not the same.\"; Break}<br \/>\n                 14 { $Result=\"The directory is not empty.\"; Break}<br \/>\n                 15 { $Result=\"There has been a sharing violation.\" ; Break}<br \/>\n                 16 { $Result=\"The start file specified was invalid.\"; Break}<br \/>\n                 17 { $Result=\"A privilege required for the operation is not held.\"; Break}<br \/>\n                 21 { $Result=\"A parameter specified is invalid.\"; Break}<\/p>\n<p>            } #end Switch<\/p>\n<p>            #display a warning message if there was a problem<br \/>\n            if ($rc.returnvalue -ne 0) {<br \/>\n              $msg=\"Error compressing: {0}. {1}\" -f $file.name,$Result<br \/>\n              Write-Warning $msg<br \/>\n              $iFailures++<br \/>\n            }<br \/>\n            Write-Verbose \"Result=$result\"<br \/>\n           } #if should process<br \/>\n    } #foreach<\/p>\n<p>    #get the time after all files have been compressed\/uncompressed<br \/>\n    $End=Get-Date<br \/>\n    [string]$RunTime=($End=$Start)<br \/>\n    Write-Verbose \"Presenting summary\"<\/p>\n<p>    Write-Progress -Activity \"Compress File Types\" -status \"Finished\" -completed $True<\/p>\n<p>    Write-Host \"`n$Status Summary\" -ForegroundColor CYAN<br \/>\n    Write-Host \"**************************************\" -ForegroundColor CYAN<br \/>\n    Write-Host \"Computer         : $computername\" -foregroundcolor CYAN<br \/>\n    Write-Host \"File types       : $extensions\" -foregroundcolor CYAN<br \/>\n    Write-Host \"Minimum Size     : $size\" -ForegroundColor CYAN<br \/>\n    Write-Host \"Path             : $path\" -foregroundcolor CYAN<br \/>\n    Write-Host \"Total Files      : $filecount\" -foregroundcolor CYAN<br \/>\n    Write-Host \"Success          : $iCompressed\" -foregroundcolor CYAN<br \/>\n    Write-Host \"Failures         : $iFailures\" -foregroundcolor CYAN<br \/>\n    Write-Host \"Runtime          : $Runtime\" -ForegroundColor CYAN<\/p>\n<p>}<br \/>\nelse {<br \/>\n    Write-Warning \"No matching files ($extensions) found in $Path on $computername with a minimum size of $size.\"<br \/>\n}<\/p>\n<p>#end of script<br \/>\n[\/cc]<\/p>\n<p>The script takes an array of file extensions, without the periods and a path. The default is the entire C:\\ drive. Optionally, you can specify a remote computer.<\/p>\n<p>[cc lang=\"DOS\"]<br \/>\nPS C:\\> c:\\scripts\\compress-filetype \"txt,\",\"ps1\",\"log\" -path c:\\files -size 10kb -comp FILE01<br \/>\n[\/cc]<\/p>\n<p>The script parses the parameters into a WMI filter. The resulting files are processed one at at time and I invoke the Compress() method for each file. I could have used Invoke-WMIMethod, but I wrote my own -Whatif code because I want to do a number of things with each file if it is compressed such as keeping a running tally of successes and failure. Also note my use of the Switch construct to decode the result value. At the end of the script, a summary is written using Write-Host. The script's pipelined output is a subset of file information.<\/p>\n<p>[cc lang=\"PowerShell\"]<br \/>\n#write the WMI file object to the pipeline<br \/>\nwrite $file | select @{Name=\"Computername\";Expression={$_.CSName}},Name,FileSize,Extension<br \/>\n[\/cc]<\/p>\n<p>Even though this is a script and not a function, you can still take advantage of cmdletbinding so you get features like -Verbose and -Whatif.<\/p>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype-300x212.png\" alt=\"\" title=\"compress-filetype\" width=\"300\" height=\"212\" class=\"aligncenter size-medium wp-image-1669\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype-300x212.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype-1024x726.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype.png 1199w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The complete file includes comment based help. Download <a href='http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/Compress-FileType.txt' target='_blank'>Compress-FileType<\/a> and let me know what you think.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I never seem to build my test virtual machines with enough disk space. Which means at some point I start dealing with space issues and resort to all sorts of hacks to buy myself a little time. One thing I do is look for files that I can compact using NTFS compression. For examples text&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,75,19],"tags":[324,534,323,547],"class_list":["post-1667","post","type-post","status-publish","format-standard","hentry","category-powershell","category-powershell-v2-0","category-wmi","tag-cim_datafile","tag-powershell","tag-scriptng","tag-wmi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Compress Files By Extension &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Compress Files By Extension &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"I never seem to build my test virtual machines with enough disk space. Which means at some point I start dealing with space issues and resort to all sorts of hacks to buy myself a little time. One thing I do is look for files that I can compact using NTFS compression. For examples text...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-03T19:41:26+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype-300x212.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Compress Files By Extension\",\"datePublished\":\"2011-10-03T19:41:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/\"},\"wordCount\":1074,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/compress-filetype-300x212.png\",\"keywords\":[\"CIM_DATAFILE\",\"PowerShell\",\"Scriptng\",\"WMI\"],\"articleSection\":[\"PowerShell\",\"PowerShell v2.0\",\"WMI\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/\",\"name\":\"Compress Files By Extension &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/compress-filetype-300x212.png\",\"datePublished\":\"2011-10-03T19:41:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/compress-filetype.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/compress-filetype.png\",\"width\":\"1199\",\"height\":\"851\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1667\\\/compress-files-by-extension\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Compress Files By Extension\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Compress Files By Extension &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/","og_locale":"en_US","og_type":"article","og_title":"Compress Files By Extension &#8226; The Lonely Administrator","og_description":"I never seem to build my test virtual machines with enough disk space. Which means at some point I start dealing with space issues and resort to all sorts of hacks to buy myself a little time. One thing I do is look for files that I can compact using NTFS compression. For examples text...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/","og_site_name":"The Lonely Administrator","article_published_time":"2011-10-03T19:41:26+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype-300x212.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Compress Files By Extension","datePublished":"2011-10-03T19:41:26+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/"},"wordCount":1074,"commentCount":7,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype-300x212.png","keywords":["CIM_DATAFILE","PowerShell","Scriptng","WMI"],"articleSection":["PowerShell","PowerShell v2.0","WMI"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/","name":"Compress Files By Extension &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype-300x212.png","datePublished":"2011-10-03T19:41:26+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/compress-filetype.png","width":"1199","height":"851"},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1667\/compress-files-by-extension\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Compress Files By Extension"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":530,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/530\/putting-the-squeeze-on-files-with-powershell\/","url_meta":{"origin":1667,"position":0},"title":"Putting the Squeeze on Files with PowerShell","author":"Jeffery Hicks","date":"December 9, 2009","format":false,"excerpt":"My December Mr. Roboto column is now online This month\u2019s tool is a PowerShell WinForm script that uses WMI to compress files. I used PrimalForms 2009 to build the graphical interface. The interface is essentially a wizard that lets you build a WMI query to find files and compress them.\u00a0\u2026","rel":"","context":"In &quot;Mr. Roboto&quot;","block_context":{"text":"Mr. Roboto","link":"https:\/\/jdhitsolutions.com\/blog\/category\/mr-roboto\/"},"img":{"alt_text":"captured_Image.png","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2009\/12\/captured_Image.png_thumb.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2760,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2760\/find-files-with-wmi-and-powershell\/","url_meta":{"origin":1667,"position":1},"title":"Find Files with WMI and PowerShell","author":"Jeffery Hicks","date":"January 29, 2013","format":false,"excerpt":"Finding files is one of those necessary evils for IT Pros. Sometimes we're searching for a needle in a haystack. And it gets even more complicated when the haystacks are on 10 or 100 or 1000 remote computers. You might think using Get-ChildItem is your only option. Certainly it works,\u2026","rel":"","context":"In &quot;Scripting&quot;","block_context":{"text":"Scripting","link":"https:\/\/jdhitsolutions.com\/blog\/category\/scripting\/"},"img":{"alt_text":"magnifying-glass-text-label-search","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/01\/magnifying-glass-text-label-search-150x150.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1025,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1025\/out-compressedfile\/","url_meta":{"origin":1667,"position":2},"title":"Out-CompressedFile","author":"Jeffery Hicks","date":"December 6, 2010","format":false,"excerpt":"I'm not sure where this idea came from, but I thought it might be nice to redirect output to a compressed text file. I know disk space is cheap these days but perhaps you're running PowerShell 2.0 on an older platform and you want to save output from a command\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/12\/out-compressedfile-1024x619.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/12\/out-compressedfile-1024x619.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/12\/out-compressedfile-1024x619.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3555,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3555\/get-powershell-version-with-wmi\/","url_meta":{"origin":1667,"position":3},"title":"Get PowerShell Version with WMI","author":"Jeffery Hicks","date":"November 13, 2013","format":false,"excerpt":"With the release of PowerShell 4.0, it is possible you might end up with a mix of systems in your environment. I know I do because I do a lot of writing, testing and development that requires multiple versions in my test network. Recently I was doing some Group Policy\u2026","rel":"","context":"In &quot;Group Policy&quot;","block_context":{"text":"Group Policy","link":"https:\/\/jdhitsolutions.com\/blog\/category\/group-policy\/"},"img":{"alt_text":"get-wmipshell","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/11\/get-wmipshell-1024x244.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/11\/get-wmipshell-1024x244.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/11\/get-wmipshell-1024x244.png?resize=525%2C300 1.5x"},"classes":[]},{"id":7835,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7835\/finding-zombie-files-with-powershell\/","url_meta":{"origin":1667,"position":4},"title":"Finding Zombie Files with PowerShell","author":"Jeffery Hicks","date":"October 30, 2020","format":false,"excerpt":"Since this is Halloween weekend in the United States, I thought I'd offer up a PowerShell solution to a scary task - finding zombie files. Ok, maybe these aren't really living dead files, but rather files with a 0-byte length. It is certainly possible that you may intentionally want a\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":2773,"url":"https:\/\/jdhitsolutions.com\/blog\/wmi\/2773\/find-files-with-wmi-and-powershell-revisited\/","url_meta":{"origin":1667,"position":5},"title":"Find Files with WMI and PowerShell Revisited","author":"Jeffery Hicks","date":"February 4, 2013","format":false,"excerpt":"Last week I posted a PowerShell function to find files using WMI. One of the comments I got was about finding files with wildcards. In WMI, we've been able to search via wildcards and the LIKE operator since the days of XP. In a WMI query we use the %\u2026","rel":"","context":"In &quot;WMI&quot;","block_context":{"text":"WMI","link":"https:\/\/jdhitsolutions.com\/blog\/category\/wmi\/"},"img":{"alt_text":"computereye","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/computereye-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1667"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1667\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}