{"id":15083,"date":"2010-02-03T07:00:00","date_gmt":"2010-02-03T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/02\/03\/what-is-the-maximum-length-of-an-environment-variable\/"},"modified":"2021-07-08T06:41:59","modified_gmt":"2021-07-08T13:41:59","slug":"20100203-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100203-00\/?p=15083","title":{"rendered":"What is the maximum length of an environment variable?"},"content":{"rendered":"<p>A customer noticed that environment variables were being truncated at 2047 characters and wanted to know what the maximum length of an environment variable is.<\/p>\n<p>This is one of those cases where you can answer the customer&#8217;s question or you can try to answer the question the customer is <i>really<\/i> asking. If you just answer the question, you&#8217;re doing the customer a disservice.<\/p>\n<p>The theoretical maximum length of an environment variable is around 32,760 characters. However, you are unlikely to attain that theoretical maximum in practice.<\/p>\n<p>All environment variables must live together in a single environment block, which itself has a limit of 32767 characters. But that count is the sum over all environment variable names and values, so you could, I guess, hit that theoretical maximum length if you deleted all the environment variables and then set a single variable called <code>X<\/code> with that really huge 32,760-character value. In practice, of course, you have to share the environment block with all the other variables in the block, so your random call to <code>SetEnvironmentVariable<\/code> with a 32,760-character string is unlikely to succeed.<\/p>\n<p>But that&#8217;s not your only practical limit.<\/p>\n<p>It also depends on how you&#8217;re setting the variable; i.e., the code that your environment-variable-setting technique passes through before it gets to the <code>SetEnvironmentVariable<\/code> call. If you&#8217;re using a batch file, then you&#8217;re constrained by <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20031210-00\/?p=41553\"> the maximum command line length<\/a> since the environment variable needs to fit into the command line buffer of the batch processor. On the other hand, maybe you&#8217;re setting the Environment registry key, in which case <a href=\"http:\/\/web.archive.org\/web\/20071020044153\/http:\/\/support.microsoft.com\/kb\/906469\"> you run into a 2048-character limit<\/a> in the code that parses that registry key and builds an environment block out of it. There&#8217;s also a limitation in the dialog box for interactively setting environment variables, the numeric value of which I don&#8217;t happen to know off the top of my head.<\/p>\n<p>This is one of those skills you have to develop when answering questions from customers: Taking the customer&#8217;s question and trying to figure out what their <i>real<\/i> question is.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Depends on who is setting it.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[104],"class_list":["post-15083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-tipssupport"],"acf":[],"blog_post_summary":"<p>Depends on who is setting it.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=15083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15083\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=15083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=15083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=15083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}