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:

Here’s the code I’m using:

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.



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


Powershell EncodedCommand Length Issue by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply