PowerShell Genie

ConvertTo-SecureString

Use ConvertTo-SecureString to turn plain text into a SecureString for credentials — or to decrypt a stored secure string back into a credential.

Command 4 min read PowerShell 7 on Windows

Documentation: Microsoft Learn — convertto-securestring

Syntax

1PS> ConvertTo-SecureString [-String] <String> [-AsPlainText] [-Force] [<CommonParameters>]
2PS> ConvertTo-SecureString [-String] <String> [-Key <Byte[]>] [<CommonParameters>]
3PS> ConvertTo-SecureString [-String] <String> [-SecureKey <SecureString>] [<CommonParameters>]

Try it

Windows PowerShell

PowerShell example · browser support is limited.

Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

Interactive simulation · fictional data

Examples

Convert plain text to a secure string

1PS> $sec = ConvertTo-SecureString 'TempPass123!' -AsPlainText -Force
Show reference output

Illustrative output · values vary by environment

(no output; $sec now holds a SecureString)

Build a credential from a secure string

1PS> $sec = ConvertTo-SecureString 'TempPass123!' -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential('CONTOSO\admin', $sec)
Show reference output

Illustrative output · values vary by environment

(credential object ready for -Credential parameters)

Store an encrypted password and load it back

1PS> Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File C:\secure\pw.txt $sec = Get-Content C:\secure\pw.txt | ConvertTo-SecureString
Show reference output

Illustrative output · values vary by environment

(the file holds encrypted text, readable only by the same user on the same computer)

Parameters

Parameter definitions:

  • -String — The text to convert. With -AsPlainText it is a readable password; otherwise it is an encrypted standard string.
  • -AsPlainText -Force — Required together to convert readable text. The -Force acknowledges the security risk.
  • -Key — A 16, 24, or 32-byte key used to encrypt or decrypt the string, so it can move between computers.
  • -SecureKey — A SecureString holding the encryption key, instead of a plain byte array.

Microsoft Learn — convertto-securestring

AsPlainText in a script defeats the whole point

ConvertTo-SecureString -AsPlainText -Force is fine for a quick interactive test. In a saved script it is worse than useless: the password sits in plain text in the file, and the SecureString wrapper only hides it from casual screen-reading. Anyone who opens the script has the password.

For unattended scripts, the safe pattern is: once, interactively, run Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File, then at runtime load it with Get-Content | ConvertTo-SecureString. The file is encrypted with your Windows account, so only the same user on the same computer can decrypt it.

If the credential must work on another computer or under another account, encrypt with an explicit -Key instead. Guard that key as carefully as the password itself, because anyone with the key and the file can decrypt the password.

Prompt for credentials with Get-Credential

Read passwords securely with Read-Host

Updated 25 October 2026. Browser outputs are fictional; check the installed module help for your environment.

Recap

ConvertTo-SecureString Use ConvertTo-SecureString to turn plain text into a SecureString for credentials — or to decrypt a stored secure string back into a credential.

Next: Add Member
Genie guideConvertTo-SecureString

ConvertTo-SecureString is the bridge between plain-text passwords and PowerShell's secure credential objects: build a PSCredential, store an encrypted password in a file, and load it back later without ever writing it in clear text. The console below is pre-loaded — run it, then change a value and run it again.