PoshCode Archive  Artifact Content

Artifact f7ad7bc56dded14acbd39c8cc0dc0cabc456ee3305cd0882447981b5f4695856:

  • File Set-FolderCompression.ps1 — part of check-in [9bb56139e2] at 2018-06-10 13:54:03 on branch trunk — Very simple function to improve the usability of WMI method (user: skourlatov size: 1237)

# encoding: ascii
# api: powershell
# title: Set-FolderCompression
# description: Very simple function to improve the usability of WMI method
# version: 0.1
# type: function
# author: skourlatov
# license: CC0
# function: Set-FolderCompression
# x-poshcode-id: 5364
# x-archived: 2015-01-31T20:32:37
# x-published: 2015-08-13T03:36:00
#
# Works great! _
#
Function Set-FolderCompression
{
	Param(
		[Parameter(
			Position=0,
			Mandatory=$true,
			ValueFromPipeline=$true)
		]
		[string]$Path,
		[switch]$Uncompress,
		[switch]$Recurse
	)

	$target_folder = $Path.TrimEnd(92) | gi -Force -ErrorAction 'Stop'

	if (($target_folder.Attributes -band 0x10) -ne 0x10)
	{
		throw 'The path must be a valid directory'
	}

	if ($Uncompress) { $un = 'un'} else { $un = '' }

	filter DoIt
	{
		Invoke-Expression -Command `
			"-not (Invoke-WmiMethod -Path `"\\.\root\cimv2:Win32_Directory.Name='$_'`" -Name $($un)compress).ReturnValue"
	}

	if ($Recurse)
	{
		$target_folder `
			| Get-ChildItem -Force -Directory -Recurse `
			| ForEach-Object -Process { $_.FullName | DoIt | Out-Null }
	}
	$target_folder.FullName | DoIt | Out-Default
}
Set-Alias -Name 'press' -Value 'Set-FolderCompression'