适用于Powershell3.0及以后版本。假设你需要给文件加密,下面教你如何给自己的文件加密:

$Path = "$env:tempsecret.txt"$Secret = 'Hello World!'$Passphrase = 'Some secret key'$key = [Byte[]]($Passphrase.PadRight(24).Substring(0,24).ToCharArray()

)$Secret |ConvertTo-SecureString -AsPlainText -Force |ConvertFrom-SecureString -Key $key |Out-File -FilePath $Pathnotepad $Path当你需要解密出里面的内容,这时就需要最初的密码:$Passphrase = Read-Host 'Enter the secret pass phrase'$Path = "$env:tempsecret.txt"$key = [Byte[]]($Passphrase.PadRight(24).Substring(0,24).ToCharArray()

)$cred = New-Object -TypeName System.Management.Automation.PSCredential('dummy', $decryptedTextSecureString)$decryptedText = $cred.GetNetworkCredential().Password