Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, November 6, 2017

How to Compress an NTFS Folder with PowerShell

Yes, I am actually taking a few minutes to write a blog entry.  I was taking a year off but I'm having to much fun teaching my 90th PowerShell class right now.  Here is a quick and easy question.  "How do I compress a folder with PowerShell?"

We have a target of E:\Mod01.  Here is the command.

Get-CimInstance -Query "SELECT * FROM CIM_Directory WHERE Name = 'E:\\Mod01'" |
    Invoke-CimMethod -MethodName Compress


The CIM_Directory class has all that we need.  The filter of 'E:\\Mod01' is not a typo.  We need to escape the backslash with another backslash.  This isolate the target so we do not compress other folders.  This object is passed to Invoke-CimMethod  where we call the Compress method. 

Nice and easy.  Remember to always verify that you are executing methods only against your intended target or really bad things can happen.




No comments: