Encode Base64

I have the following script :

$ args = @ ’
powershell.exe-Command {

set-ItemProperty-Path 'hklm: \ \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ Microsoft.PowerShell '-Name “ExecutionPolicy”-Value “RemoteSigned”
}
'@
Start-Process-ArgumentList $ args-Verb RunAs PowerShell

I would like to apply base64 encoding and then use the result in a batch file like this:
powershell -encodeCommad
I can not make it work.
Help, please!
Alternatively, any method that put PowerShell code in batch / cmd file without having to diner to separate. .ps1 file

If I understand you correctly, here’s what you’re looking for:

$command = {
$args = @'
-Command "Set-ItemProperty -Path 'hklm:\\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\' -Name ExecutionPolicy -Value RemoteSigned"
'@

Start-Process -ArgumentList $args -Verb RunAs PowerShell 
}.ToString()

$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))

"powershell.exe -EncodedCommand $encodedCommand" | clip.exe

Normally I would build a here-string with the command to be encoded, but in this case, that was awkward because your command already had a here-string inside it. Instead, I used a trick of putting all that code into a ScriptBlock and then converting that back into a string. The last two lines are the most important part, showing how to convert a string into a value that you can pass to powershell.exe’s -EncodedCommand parameter. The last line outputs the entire command to the clipboard, so you can paste it into a batch file.

Wonderful!
There are three days that I lose my mind …
Thanks!!!

I am a newbie to powershell, so sorry for the mistakes …
I have read this post and I wanted to apply the proposed solution by Dave Wyatt for this script:
$ args = @ ’
powershell.exe-Command {
ECHO Y | takeown / f “c: \ users \ test” / r | out-null
icacls “c: \ users \ test” / grant administrators: F / t | out-null
}
'@
Start-Process-ArgumentList $ args-Verb RunAs PowerShell-WindowStyle Hidden
$ computers = Get-WmiObject-Class Win32_ComputerSystem | out-file “C: \ youfilename.txt”
$ EMAILTO = “xxxx@gmail.com
$ Emailfrom = “me@mydomain.com
$ Subject = “Test”
$ Body = “Test Body”
$ SMTPServer = “smtp.gmail.com
$ filenameAndPath = “C: \ youfilename.txt”
$ SMTPMessage = New-Object System.Net.Mail.MailMessage ($ emailfrom, $ EMAILTO, $ Subject, $ Body)
$ attachment = New-Object System.Net.Mail.Attachment ($ filenameAndPath)
$ SMTPMessage.Attachments.Add ($ attachment)
$ SMTPClient = New-Object Net.Mail.SmtpClient ($ SmtpServer, 587)
$ SMTPClient.EnableSsl = $ true
$ SMTPClient.Credentials = New-Object System.Net.NetworkCredential (“xxxx”, “yyyyyyy”);
$ SmtpClient.Send ($ SMTPMessage)

remove-item-force C: \ youfilename.txt
Cmd / c “rmdir / S / Q c: \ Users \ test”

the scipt works but the Base 64 encoding is not working …

test in this way. It should work …:

$command = {
$args = @’
-Command “ECHO S | takeown /f “c:\users\test” /r | out-null; icacls “c:\userstest” /grant administrators:F /t | out-null”
'@

Start-Process -ArgumentList $args -Verb RunAs PowerShell -WindowStyle Hidden
}.ToString()

$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))

“powershell.exe -EncodedCommand $encodedCommand” | clip.exe

$command1 = {
$computers = Get-WmiObject -Class Win32_ComputerSystem | out-file “C: \ youfilename.txt”
$EMAILTO = “xxxx@gmail.com
$Emailfrom = “me@mydomain.com
$Subject = “Test”
$Body = “Test Body”
$SMTPServer = “smtp.gmail.com
$filenameAndPath = “C: \ youfilename.txt”
$SMTPMessage = New-Object System.Net.Mail.MailMessage($emailfrom, $EMAILTO, $Subject, $Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“xxxx”, “yyyyy”)
$SmtpClient.Send($SMTPMessage)
}.ToString()

$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command1))

“powershell.exe -EncodedCommand $encodedCommand” | clip.exe

Thanks@Dave Wyatt!