Encrypting powershell script using key parameter to decrypt and run on other computers

This seems like a strange workflow . Can you clarify on why you need to be encrypting a script like this? I think from a general perspective you need to re-evaluate why you are doing this process, as and make sure you’re actually solving a problem.

As for the issue - I’m not quite following the logic on why you or the person who was helping you is passing ConvertTo-SecureString -String $Key -AsPlainText -Force to the Key parameter. Also you need a securestring already to Convert something from a secure string, which you do in the first code that worked, but you didn’t from the other person’s example. It doesn’t seem like that person even tested their code heh.

ConvertTo-SecureString (Microsoft.PowerShell.Security) - PowerShell | Microsoft Learn

couple of examples on that page, you probably can just follow those to accomplish the task. I wrote this real quick which seems to work.

$Key = (1..16)
$Code = Get-Content -Raw ".\scriptdata.txt"
$SecureCode = ConvertTo-SecureString -String $Code -AsPlainText -Force
$Encrypted = ConvertFrom-SecureString -SecureString $SecureCode -Key $Key
$Encrypted | Out-File -FilePath ".\scriptdataEncrypted.txt"