To base64 encode a PowerShell command, convert the command string into UTF-16LE bytes and then encode those bytes using Base64. This is commonly used to obfuscate commands for legitimate scripting or forensic analysis, and requires PowerShell (built-in on Windows) and no extra tools.
What You'll Need
- Windows with PowerShell installed (PowerShell 3.0 or later, included by default on Windows 8/Server 2012 and newer)
- Administrator privileges are not required for encoding itself, but may be needed depending on what the encoded command does when executed
- Access to a PowerShell terminal (run as your user account)
Step-by-Step
- Open PowerShell: Press
Win + X, then select Windows PowerShell or Terminal (if PowerShell is configured there). Alternatively, search for "PowerShell" in the Start menu and open it. Use the x86 (32-bit) version only if your target environment is 32-bit; otherwise, use the standard x64 PowerShell. - Prepare your command as a string: Type or paste the full PowerShell command you want to encode. For example:
Get-Process | Where-Object { $_.CPU -gt 100 }. Make sure the command works when typed directly into PowerShell before encoding it. - Convert the string to UTF-16LE bytes: PowerShell cmdlets handle encoding natively. Run this one-liner to get the byte array:
[System.Text.Encoding]::Unicode.GetBytes($command). However, since the variable$commanddoesn't exist yet, first assign your command to a variable. For a single-step approach, use:$cmd = 'Get-Process | Where-Object { $_.CPU -gt 100 }' - Encode the bytes to Base64: Pipe the byte array into Base64 encoding:
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($cmd)). PressEnterafter each line. The output will be a long string of letters, numbers, and symbols—this is your encoded command. - Store or use the encoded string: Copy the Base64 output. To execute later, you’ll use the
-EncodedCommandparameter in PowerShell:powershell.exe -EncodedCommand <Base64String>. For example, if the encoded string isRwBlAHQALQBXAGgAaQBsAGUATwBiAGoAZQBjAHQA..., run:powershell.exe -EncodedCommand RwBlAHQALQBXAGgAaQBsAGUATwBiAGoAZQBjAHQA...
Common Mistakes
- Using ASCII instead of UTF-16LE: PowerShell’s
-EncodedCommandparameter expects UTF-16LE (little-endian Unicode). Encoding with ASCII or UTF-8 will cause errors like "The term '...' is not recognized". Always use[System.Text.Encoding]::Unicode, which is UTF-16LE in .NET. - Forgetting the single quotes around multi-word commands: If your command includes spaces or special characters, omitting quotes in the variable assignment (e.g.,
$cmd = Get-Process |...without quotes) will fail parsing. Always wrap the full command in single quotes. - Running the encoded command in the wrong PowerShell host: Use
powershell.exe(the classic console host), notpwsh.exe(PowerShell Core/7+), unless you explicitly encode using UTF-8 and adjust the encoding parameter. Legacy-EncodedCommandworks only withpowershell.exeand expects UTF-16LE. - Escaping quotes inside the command: If your command contains single quotes (e.g.,
'user name'), escape them by doubling them inside the outer single-quoted string:$cmd = 'Write-Host ''Hello'' '. Alternatively, use double quotes for the outer assignment and escape inner double quotes with backticks (`").
Tips
- Test the encoded command immediately: After encoding, run
powershell.exe -EncodedCommand <yourString>right away to verify it executes correctly. This catches encoding errors before deployment. - Use a script block for complex commands: For very long or nested commands, define them in a script block first:
$sb = { Get-Process | Select-Object Name, CPU }, then $cmd = $sb.ToString(). This avoids quote/escape issues. - Check for hidden characters: Copy-pasting from web pages or Word documents can introduce invisible Unicode characters (like zero-width spaces). Retype the command manually if encoding fails unexpectedly.
- Encode only the command body: Do not include
powershell.exeor-EncodedCommandin the string. Encode only the command logic (e.g.,Get-Process, notpowershell.exe -c Get-Process).
Troubleshooting
If the encoded command fails when executed, follow these steps:
- Verify the encoding method: Re-run the encoding command in PowerShell and confirm you used
[System.Text.Encoding]::Unicode.GetBytes(...). If you used UTF8, re-encode using Unicode. - Reproduce the command manually: Paste the original (unencoded) command directly into PowerShell to ensure it runs without error. If it fails unencoded, fix that first.
- Check for truncation: Some terminals or logs truncate long Base64 strings. Re-encode and copy the full output—don’t retype it. Use
Write-Host $encodedStringand select-all to ensure nothing is cut off. - Confirm the PowerShell host: Ensure you’re using
powershell.exe, notpwsh.exe. In Windows Terminal, select the classic PowerShell profile. Run$PSVersionTablein the host: ifPSEditionisCore, switch toDesktop(i.e.,powershell.exe). - Inspect the Base64 string: Decode it back to verify correctness:
[Text.Encoding]::Unicode.GetString([Convert]::FromBase64String('YOURSTRING')). The output should match your original command exactly, including spaces and quotes.
Frequently Asked Questions
Can I encode PowerShell commands on non-Windows systems?
No. The -EncodedCommand parameter and UTF-16LE requirement are specific to Windows PowerShell (powershell.exe). While PowerShell Core (Linux/macOS) supports -EncodedCommand, it expects UTF-8, not UTF-16LE. For cross-platform use, encode with UTF-8 and use pwsh.exe, but this is not compatible with the Windows classic host.
Why does my encoded command show "Access is denied"?
Access denied usually indicates a permissions issue, not an encoding problem. The encoded command itself runs with your user context. Check if the underlying command requires elevated privileges (e.g., modifying system files or services) and run PowerShell as Administrator. Also verify Execution Policy: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser may be needed if unsigned scripts are blocked.
Is Base64 encoding secure for hiding sensitive commands?
No. Base64 is encoding, not encryption—it’s easily reversible. Anyone with the encoded string can decode it with a simple PowerShell command. Never use it to hide credentials or secrets. For real security, use encrypted credential storage (like Export-Clixml with a key) or Windows DPAPI.
How do I encode a command that includes variables or logic?
Define the full logic as a string before encoding. For example: $cmd = 'param($Name) Get-Service -Name $Name', then encode $cmd. When executing, pass arguments using -ArgumentList: powershell.exe -EncodedCommand <base64> -ArgumentList 'Spooler'.
Can I encode multi-line commands?
Yes. Include line breaks in your string using `n (backtick-n). For example: $cmd = 'Get-Process`nGet-Service'. When decoded, PowerShell will treat it as two separate commands separated by a newline. You can also use semicolons: $cmd = 'Get-Process; Get-Service'.
What if I get "The term '...' is not recognized" after decoding?
This usually means the command was truncated, had invisible characters, or was encoded with the wrong encoding. Double-check the original string for typos, and re-encode using the exact Unicode method. Also, ensure the target system has the required modules or snap-ins loaded (e.g., Import-Module ActiveDirectory may be needed before Get-ADUser).