Share via


what is the maximum number of characters or limit for powershell string variable we can pass

Question

Wednesday, April 13, 2016 12:43 PM

hi,

Can any one suggest on below.

what is the maximum number of characters or length a PowerShell string variable can accommodate.

[string]$TestCharacterlimit

Thanks & Regards, Krishna

All replies (3)

Thursday, April 14, 2016 2:34 AM âś…Answered | 1 vote

Hi Krishna,

According to your description, you want to know the max length of a PowerShell string variable.

Strings in PowerShell (and most of the other programming languages) are of "Fixed length" or immutable (they cannot change). .net also offers a mutable string object System.Text.StringBuilder, which should be your preference if you change the string a lot through out its lifetime.

The theoretical max length of a String is 2147483647, but you are more likely to run out of memory before you ever reach that limit.

This will show the maximum length:

$s = New-Object Text.StringBuilder "test"
$s.MaxCapacity

Best Regards,

CY

TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected].


Wednesday, April 13, 2016 1:03 PM

Post it PS forum for more audience.

Rajesh | Blog


Wednesday, April 13, 2016 2:33 PM | 1 vote

The Memory restrictions aside, Strings cannot have more than 2<sup>30</sup> (1,073,741,824) characters, since a 2GB limit is imposed by the Microsoft CLR (Common Language Runtime).

Jerry Yasir - Office Server & Services MVP/MCT Hewlett Packard Enterprise - If this reply helped you resolve your issue, please propose as answer. It may help other community members. Thanks!