Compress a folder with powershell

Viewed 209

I have a folder that I want automatically compress with a script to make space. For that, I use the following script for a folder called 'test':

$a = Get-Item "D:\Aither\Pca\Test"
$a.Attributes += "Compressed" 

After some tests, the attribute "hidden" works with the previous code but not with the attribute "Compressed"

enter image description here

Compress contents to save disk space should be checked. Even when turning off my computer, it's not working.

Did I make a mistake in my code or it's just a rights issue ?

2 Answers

Additionally to your answer -- there is a native command

Enable-NtfsCompression [-Path] <String[]> [-Recurse] [-WhatIf] [-Confirm] [<CommonParameters>]

I found a answer with the following code :

Invoke-WmiMethod -Path "Win32_Directory.Name='D:\Aither\Pca\Test'" -Name compress
Related