Q:

Powershell EncodedCommand Length Issue

I'm trying to use powershell.exe's -EncodedCommand parameter to run a powershell script as a different user.  I'm doing this to avoid dealing with command line difficulties of escaping quotes and other special characters.  I've found that when the length of the encoded command exceeds 916 characters, it fails with the following message:
Start-Process : This command cannot be run due to the error: The stub received bad data.

Here's the code I'm using:
$path = 'c:\temp'

$UserName = '.\someuser'
$Password = 'somepassword'
$securePassword = ($Password | ConvertTo-SecureString -AsPlainText -Force)
$credential = New-Object System.Management.Automation.PSCredential $UserName, $securePassword

$command = {& .\Restore-DatabaseFromBackupFile.ps1 -DatabaseName 'aaaaaaaaaaaaaaa' -DatabaseBackupFilePath 'aaaaaaaaaaaaaaaaaaaaaaaaaaa' -DatabaseDataLogicalName 'aaaaaaaaaaaaaaa' -DatabaseLogLogicalName 'aaaaaaaaaaaaaaaaaaa' -DatabaseDataFilePath 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef' -DatabaseLogFilePath 'a';Start-Sleep -Seconds 2}
$commandBytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($commandBytes)
Write-Warning $encodedCommand.Length
Write-Warning $encodedCommand
Start-Process -FilePath 'powershell.exe' -ArgumentList "-ExecutionPolicy Unrestricted -EncodedCommand $encodedCommand" -WorkingDirectory $path -LoadUserProfile -Credential $Credential

This code will fail.  However, if you remove a single character from any of the parameter values (or shorten the command within the script block in any way), it will execute successfully.
I've found references to there being an 8190 character limit to cmd.exe on Windows XP and newer (this is running on Windows Server 2012 R2), but I seem to be a long way from this limit.
Any ideas?
**UPDATE1: This behavior is affected by the -Credential parameter.  If I remove the -Credential parameter and run as the current user, I've been able to successfully execute the command when the encoded command length is over 19,000.
**UPDATE2: @Xalorous's comments did solve the issue. Setting the ExecutionPolicy to Bypass.  I would be happy to give credit if he/she would post the answer.
Thanks!

A:

Try -ExecutionPolicy Bypass instead of Unrestricted. Unrestricted makes sense for setting machine or user policy, local or GPO. It makes less sense for the commandline

