Encryption.psm1
#region Decrypt-File function Decrypt-File { <# .Synopsis Decrypt a file. .DESCRIPTION Decrypt a file using EFS. .EXAMPLE Decrypt-File -Path .\test.txt This command will decrypt the test.txt file. #> [CmdletBinding(SupportsShouldProcess=$true, PositionalBinding=$false, ConfirmImpact='Medium')] [Alias()] [OutputType([String[]])] Param ( # The path to the file to be encrypted [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Alias("File")] [string[]]$Path ) Begin{} Process { foreach($p in $Path) { if( (Test-Path -Path $p)) { if ($pscmdlet.ShouldProcess($p)) { $fullPath = (Resolve-Path $p).Path Write-Verbose "Decrypting file $p" [System.IO.file]::Decrypt($fullPath) } } else { Throw "Could not find file $p" } } } End{} } #endregion #region Encrypt-File function Encrypt-File { <# .Synopsis Encrypt a file. .DESCRIPTION Encrypt a file using EFS. .EXAMPLE Encrypt-File -Path .\test.txt This command will encrypt the file test.txt. #> [CmdletBinding(SupportsShouldProcess=$true, PositionalBinding=$false, ConfirmImpact='Medium')] [Alias()] [OutputType([String[]])] Param ( # The path to the file to be encrypted [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Alias("File")] [string[]]$Path ) Begin{} Process { foreach($p in $Path) { if( (Test-Path -Path $p)) { if ($pscmdlet.ShouldProcess($p)) { $fullPath = (Resolve-Path $p).Path Write-Verbose "Encrypting file $p" [System.IO.file]::Encrypt($fullPath) } } else { Throw "Could not find file $p" } } } End{} } #endregion #region Decrypt-Folder function Decrypt-Folder { <# .Synopsis Decrypt a folder. .DESCRIPTION Decrypt a folder encrypted with EFS. .EXAMPLE Decrypt-Folder -Path .\Test This command will decrypt the folder Test. .EXAMPLE Decrypt-Folder -Path .\Test -Recurse This command will decrypt the folder test and all subfolders and files recursively. #> [CmdletBinding(SupportsShouldProcess=$true, PositionalBinding=$false, ConfirmImpact='Medium')] [Alias()] [OutputType([String[]])] Param ( # The path to the file to be encrypted [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Alias("File")] [string[]]$Path, [switch]$Recurse ) Begin{} Process { foreach($p in $Path) { if( (Test-Path -Path $p)) { if ($pscmdlet.ShouldProcess($p)) { $fullPath = (Resolve-Path $p).Path Write-Verbose "Decrypting folder $p" [System.IO.file]::Decrypt($fullPath) if($Recurse) { Write-Verbose "Decrypting subfolders and files" Get-ChildItem -Path $fullPath -Directory -Force | %{ Decrypt-Folder -Path $_.FullName -Recurse } Get-ChildItem -Path $fullPath -File -Force | %{ Decrypt-File -Path $_.FullName } } } } else { Throw "Could not find folder $p" } } } End{} } #endregion #region Encrypt-Folder function Encrypt-Folder { <# .Synopsis Encrypt a folder. .DESCRIPTION Encrypt a folder using EFS. .EXAMPLE Encrypt-Folder -Path .\Test This command will encrypt the folder Test. .EXAMPLE Encrypt-Folder -Path .\Test -Recurse This command will encrypt the folder Test and all subfolders and files recursively. #> [CmdletBinding(SupportsShouldProcess=$true, PositionalBinding=$false, ConfirmImpact='Medium')] [Alias()] [OutputType([String[]])] Param ( # The path to the file to be encrypted [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Alias("Folder")] [string[]]$Path, [switch]$Recurse ) Begin{} Process { foreach($p in $Path) { if( (Test-Path -Path $p)) { if ($pscmdlet.ShouldProcess($p)) { $fullPath = (Resolve-Path $p).Path Write-Verbose "Encrypting folder $p" [System.IO.file]::Encrypt($fullPath) if($Recurse) { Write-Verbose "Encrypting subfolders and files" Get-ChildItem -Path $fullPath -Directory -Force | %{ Encrypt-Folder -Path $_.FullName -Recurse } Get-ChildItem -Path $fullPath -File -Force | %{ Encrypt-File -Path $_.FullName } } } } else { Throw "Could not find folder $p" } } } End{} } #endregion #region Exports Export-ModuleMember -Function Encrypt-File Export-ModuleMember -Function Decrypt-File Export-ModuleMember -Function Encrypt-Folder Export-ModuleMember -Function Decrypt-Folder #endregion |