{"id":8438,"date":"2022-02-01T21:19:06","date_gmt":"2022-02-01T20:19:06","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=8438"},"modified":"2022-02-02T11:28:21","modified_gmt":"2022-02-02T10:28:21","slug":"windows-set-or-edit-environment-variable-from-powershell","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/windows-set-or-edit-environment-variable-from-powershell\/","title":{"rendered":"Windows: Set or edit environment variable from powershell"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_8438 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_8438')){$('.twoclick_social_bookmarks_post_8438').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Windows%3A%20Set%20or%20edit%20environment%20variable%20from%20powershell\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Apowershell%20can%20set%2Cedit%20or%20delete%20environment%20variables%20as%20well%20as%20cmd.exe%20command%20line%20interpreter.%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/windows-set-or-edit-environment-variable-from-powershell\\\/\",\"post_id\":8438,\"post_title_referrer_track\":\"Windows%3A+Set+or+edit+environment+variable+from+powershell\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>powershell can set,edit or delete environment variables as well as cmd.exe command line interpreter.<br \/>\n<!--more--><\/p>\n<p>Powershell has no command let for this. But the .NET framework has an appropriate class [Environment].<\/p>\n<p>Some examples. The environment of an user contains two parts: The user and the system part.<\/p>\n<p>All variables can be shown by<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; &#x5B;Environment]::GetEnvironmentVariables()\r\n<\/pre>\n<p>Only the user part<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; &#x5B;Environment]::GetEnvironmentVariables(&quot;user&quot;)\r\n<\/pre>\n<p>and only the system\/machine part<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; &#x5B;Environment]::GetEnvironmentVariables(&quot;machine&quot;)\r\n<\/pre>\n<p>A single variable can be read by<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; $myPowershellModulePathes=&#x5B;Environment]::GetEnvironmentVariable(&quot;PSModulePath &quot;,&quot;user&quot;)\r\n<\/pre>\n<p>The method to set an environment variable is <code>[Environment]::SetEnvironmentVariable()<\/code>.<\/p>\n<p>For example: You want to add a additional user path, D:\\MyModules,  for powershell modules. Powershell reads the module pathes from the <strong>PSModulePath <\/strong>environment variable.<\/p>\n<p>Get the current variable and add the new path<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; $myPowershellModulePathes=&#x5B;Environment]::GetEnvironmentVariable(&quot;PSModulePath &quot;,&quot;user&quot;)\r\nPS D:\\&gt;\r\nif( &#x5B;string]::IsNullOrEmpty($myPowershellModulePathes )\r\n{\r\n      &#x5B;Environment]::SetEnvironmentVariable(&quot;PSModulePath &quot;,&quot;D:\\MyModules&quot;,&quot;user&quot;)\r\n}\r\nelse\r\n{\r\n      &#x5B;Environment]::SetEnvironmentVariable(&quot;PSModulePath &quot;, (&#x5B;string]::Join(&quot;;&quot;,@($myPowershellModulePathes,&quot;D:\\MyModules&quot;))) ,&quot;user&quot;)\r\n}\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, powershell can set,edit or delete environment variables as well as cmd.exe command line interpreter.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344,5,4],"tags":[1797,73,133,384,1799,1798],"class_list":["post-8438","post","type-post","status-publish","format-standard","hentry","category-powershell-scripting","category-windowsknowhow","category-windowsscripts","tag-environment","tag-get","tag-powershell","tag-set","tag-set-powershell-module-path","tag-variable"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=8438"}],"version-history":[{"count":6,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8438\/revisions"}],"predecessor-version":[{"id":8444,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8438\/revisions\/8444"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=8438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=8438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=8438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}