Convert PowerShell script into non-readable format
Solution 1
I tried the solution proposed by @TrevorSullivan, but it gave me error
The term '????' is not recognized as the name of a cmdlet, function,
script file or operable program...
As I found out later there was a problem with bad encoding. I found somewhere another approach and when I combined those two, I got working PS command:
$Base64 = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes([System.IO.File]::ReadAllText("script.ps1")))
Then I can redirect the result to file:
$Base64 > base64Script.txt
from where I just copy the encoded command and paste it here instead of <Base64String>
:
powershell.exe -EncodedCommand <Base64String>
and it works without any problem.
Solution 2
Thanks guys for your posts. I took @Frimlik's post and created my own script to automate the process. I hope this helps someone. Save the script to a ps1 file and run it.
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
Function EncodePS1ToBat {
$ScriptToEncode = Get-FileName
# Encode the script into the variable $Base64
$Base64 = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes([System.IO.File]::ReadAllText($ScriptToEncode)))
# Get the path and name to be used as output
$filePath = Split-Path $ScriptToEncode -Parent
$fileName = (Split-Path $ScriptToEncode -Leaf).Split(".")[0]
# Output the encoded script into a batch file on the same directory of the origial script
"@echo off`n powershell.exe -ExecutionPolicy Bypass -EncodedCommand $Base64" |
Out-File -FilePath "$filePath\$fileName`_Encoded.bat" -Force -Encoding ascii
}
# Call the funtion to encode the script to a batch file
EncodePS1ToBat
Related videos on Youtube

Nipun
Passionate about software product development and software design. Technology: C++, C++11/14, C++/CLI, VC++, UML Modeling, Multithreading, STL, IPC, Network Programming, Powershell Competencies: •Working as Senior Developer at Saxo Bank, Gurgaon. •10 years experience in software development life cycle, software design, writing technical documents, coding. •Experience in team leading, efforts estimation, risk analysis, training and customer support. •Good knowledge on scripting using DOS & Powershell. •Drafting and executing various policies at organization level like Rewards & Recognition, Technical Presentationsetc. Domain: VOIP, Industrial automation (DCS, DCS Simulator), Condition Monitoring, Investment Banking
Updated on September 14, 2022Comments
-
Nipun 8 months
I have a PowerShell script which installs a patch (contains set of files to be added) on a customer machine. For this, I have created a batch file which executes this PowerShell script.
For the customer to run this batch file, the PowerShell script file must be placed onto the customer machine as well.The PowerShell script is in text format, which can be read and understood by the customer easily.
Can we convert this script file into some non-readable format (e.g. bin or exe), so that it is not readable by the customer?
-
Knuckle-Dragger over 9 yearsIf money is no object, get Powershell Studio from Sapien. It's got a one button compile option and I've built all sorts of crappy little GUI.EXE's with it.
-