{"id":1703,"date":"2018-08-24T22:00:00","date_gmt":"2018-08-24T20:00:00","guid":{"rendered":"https:\/\/demo.sidekick.be\/itenium\/environment-variables-management-with-powershell\/"},"modified":"2025-11-28T13:31:47","modified_gmt":"2025-11-28T12:31:47","slug":"environment-variables-management-with-powershell","status":"publish","type":"post","link":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/","title":{"rendered":"Managing Environment Variables with PowerShell"},"content":{"rendered":"<p>If you\u2019d like a GUI instead you could try: <a href=\"https:\/\/github.com\/rix0rrr\/WindowsPathEditor\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/rix0rrr\/WindowsPathEditor.svg?style=social&amp;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/rix0rrr\/WindowsPathEditor\">rix0rrr\/WindowsPathEditor<\/a>:<\/p>\n<h1 id=\"getting-environment-variables\">Getting environment variables<\/h1>\n<pre><code class=\"language-powershell\"># Listing all environment variables\nGet-ChildItem Env:\nenv\n\n# Or just those containing path\nGet-ChildItem Env:*PATH* | Format-List\n\n# Display a specific one (case insensitive)\n$env:PATH\n[environment]::GetEnvironmentVariable(\"PATH\", \"Process\")\n\n# They are the union of\n# Machine: HKEY_LOCAL_MACHINESystemCurrentControlSetControlSession ManagerEnvironment\n[environment]::GetEnvironmentVariable(\"PATH\", \"Machine\")\n# User: HKEY_CURRENT_USEREnvironment\n[environment]::GetEnvironmentVariable(\"PATH\", \"User\")\n<\/code><\/pre>\n<p>In cmd.exe, all envs can be listed with <code>set<\/code>.<\/p>\n<h1 id=\"managing-environment-variables\">Managing environment variables<\/h1>\n<pre><code class=\"language-powershell\"># Managing current session environment\n$env:test = \"value\"\nRemove-Item Env:test\n\n# Persistent registry management (registry)\n[Environment]::SetEnvironmentVariable(\"yaye\", \"value\", \"User\")\n[Environment]::SetEnvironmentVariable(\"yaye\", \"$null\", \"User\")\n\n# Requires RunAs Admin\n[Environment]::SetEnvironmentVariable(\"yaye\", \"value\", \"Machine\")\n<\/code><\/pre>\n<p>When using the PowerShell <code>$env:key = \"val\"<\/code>, the environment changes are available only for the current session. When using the .NET <code>[Environment]::SetEnvironmentVariable<\/code>, the changes are persisted to the registry but they do not become available in the current session.<\/p>\n<p>A helper function to the rescue!<\/p>\n<pre><code class=\"language-powershell\">Set-Alias se Set-Environment\n\nfunction Set-Environment([String]$key, [String]$value) {\n\t[System.Environment]::SetEnvironmentVariable(\"$key\", \"$value\", \"User\")\n\tSet-Item -Path Env:$key -Value $value\n}\n<\/code><\/pre>\n<p>Already opened processes, tabs, etc won\u2019t see these environment changes. The environment can be refreshed from the registry however.<\/p>\n<pre><code class=\"language-powershell\">Set-Alias re Refresh-Environment\n\nfunction Refresh-Environment {\n\t$locations = 'HKLM:SYSTEMCurrentControlSetControlSession ManagerEnvironment', 'HKCU:Environment'\n\t$locations | ForEach-Object {\n\t\t$k = Get-Item $_\n\t\t$k.GetValueNames() | ForEach-Object {\n\t\t\t$name = $_\n\t\t\t$value = $k.GetValue($_)\n\t\t\tSet-Item -Path Env:$name -Value $value\n\t\t}\n\t}\n\n\t# Put Machine and User $env:PATH together\n\t$machinePath = [Environment]::GetEnvironmentVariable(\"PATH\", \"Machine\")\n\t$userPath = [Environment]::GetEnvironmentVariable(\"PATH\", \"User\")\n\t$Env:PATH = \"$machinePath;$userPath\"\n}\n<\/code><\/pre>\n<p>If you have chocolatey installed, <code>RefreshEnv<\/code> will also work.<\/p>\n<hr \/>\n<h1 id=\"envpath-helpers\">$env:PATH helpers<\/h1>\n<p>Use <code>fp<\/code> to list all directories in <code>$env:path<\/code>. It accepts a search needle as parameter. ex: <code>fp node<\/code>.<\/p>\n<p>By using the PowerShell builtin variable <code>$args<\/code>, it is not required to put quotes around directories containing spaces.<\/p>\n<pre><code class=\"language-powershell\">Set-Alias fp Find-EnvironmentPath\n\nfunction Find-EnvironmentPath {\n\t$needle = ($args -join \" \").TrimEnd(\"\")\n\t$needle = [Regex]::Escape($needle)\n\n\t$env:PATH.split(\";\") |\n\t\tWhere-Object { $_.TrimEnd(\"\") -match $needle } |\n\t\tSort-Object |\n\t\tGet-Unique -AsString\n}\n<\/code><\/pre>\n<p>Append a directory to the User path with <code>ap c:bins<\/code>.<\/p>\n<pre><code class=\"language-powershell\">Set-Alias ap Append-EnvironmentPath\n\nfunction Append-EnvironmentPath {\n\t$toAppend = ($args -join \" \").TrimEnd(\"\")\n\tif (-not (Test-Path $toAppend)) {\n\t\tWrite-Host \"Path doesn't exist...\"\n\t\tWrite-Host $toAppend\n\t\treturn\n\t}\n\t$env:PATH = $env:PATH + \";$toAppend\"\n\t$userPath = [Environment]::GetEnvironmentVariable(\"PATH\", \"User\")\n\t[Environment]::SetEnvironmentVariable(\"PATH\", \"$userPath;$toAppend\", \"User\")\n}\n<\/code><\/pre>\n<p>Remove a directory from the User path with <code>delp c:Program FilessomeBin<\/code>.<\/p>\n<pre><code class=\"language-powershell\">Set-Alias delp Remove-EnvironmentPath\n\nfunction Remove-EnvironmentPath {\n\t$toRemove = ($args -join \" \").TrimEnd(\"\")\n\n\t$userPath = [Environment]::GetEnvironmentVariable(\"PATH\", \"User\")\n\t$changedUserPath = $userPath.Split(\";\") |\n\t\tForEach-Object { $_.TrimEnd(\"\") } |\n\t\tWhere-Object { $_ -ne $toRemove }\n\n\t$changedUserPath = $changedUserPath -Join \";\"\n\n\tif ($userPath -ne $changedUserPath) {\n\t\t[Environment]::SetEnvironmentVariable(\"PATH\", \"$changedUserPath\", \"User\")\n\t\t$env:PATH = [Environment]::GetEnvironmentVariable(\"PATH\", \"Machine\")\n\t\t$env:PATH += \";$changedUserPath\"\n\t\treturn\n\t}\n\n\techo \"Not removed\"\n\n\t$machinePath = [Environment]::GetEnvironmentVariable(\"PATH\", \"Machine\")\n\t$isInMachine = $machinePath.split(\";\") | Where-Object { $_.Trim(\"\") -eq $toRemove }\n\tif ($isInMachine) {\n\t\techo \"Is present in Machine scope\"\n\t}\n}\n<\/code><\/pre>\n<hr \/>\n<h1 id=\"useful-environment-variables\">Useful environment variables<\/h1>\n<p>Win + Pause: <code>Control PanelSystem and SecuritySystem<\/code><br \/> PowerShell: <code>$env:USERNAME<\/code><br \/> Cmd: <code>%USERNAME%<\/code><\/p>\n<p>These are probably worth remembering.<\/p>\n<pre><code class=\"language-powershell\">\"$env:USERNAME@$env:USERDOMAIN\"\n\n$env:APPDATA\n# C:UsersWouterAppDataRoaming\n\n$env:HOME\n$env:USERPROFILE\n# C:UsersWouter\n\n$env:LOCALAPPDATA\n# C:UsersWouterAppDataLocal\n\n$env:TEMP\n# C:UsersWouterAppDataLocalTemp\n<\/code><\/pre>\n<h1>Extras<\/h1>\n<p><b>Stuff that came into being during the making of this post<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/Laoujin\/dotfiles\/blob\/master\/config\/PowerShell\/scripts\/envpath.ps1\">The full ps1 source<\/a><\/li>\n<\/ul>\n<h1>Updates<\/h1>\n<ul>\n<li> 25 August 2018<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Working with environment variables in Windows is as easy as:<\/p>\n<pre><code>Win + Pause &gt; \"Advanced system settings\" &gt; \"Environment Variables...\"\n<\/code><\/pre>\n<p>After which you get a tiny, unresizable, form where you can view and manage them. Something better eventually arrived with Windows 10 but still, PowerShell \ud83d\ude42<\/p>\n<p>Use Autohotkey to open the window with <code>Left Alt + Pause<\/code>:<\/p>\n<pre><code class=\"language-autohotkey\">LAlt &amp; Pause::Run % \"rundll32 sysdm.cpl,EditEnvironmentVariables\"\n<\/code><\/pre>\n","protected":false},"author":1,"featured_media":2122,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[5],"tags":[9,7],"class_list":["post-1703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev-setup","tag-powershell","tag-windows"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Managing Environment Variables with PowerShell - Itenium<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Managing Environment Variables with PowerShell - Itenium\" \/>\n<meta property=\"og:description\" content=\"Working with environment variables in Windows is as easy as: Win + Pause &gt; &quot;Advanced system settings&quot; &gt; &quot;Environment Variables...&quot; After which you get a tiny, unresizable, form where you can view and manage them. Something better eventually arrived with Windows 10 but still, PowerShell :)Use Autohotkey to open the window with Left Alt + Pause: LAlt &amp; Pause::Run % &quot;rundll32 sysdm.cpl,EditEnvironmentVariables&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"Itenium\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-24T20:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-28T12:31:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"360\" \/>\n\t<meta property=\"og:image:height\" content=\"203\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Usdevelop\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Usdevelop\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/\",\"name\":\"Managing Environment Variables with PowerShell - Itenium\",\"isPartOf\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg\",\"datePublished\":\"2018-08-24T20:00:00+00:00\",\"dateModified\":\"2025-11-28T12:31:47+00:00\",\"author\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\"},\"breadcrumb\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#primaryimage\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg\",\"contentUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg\",\"width\":360,\"height\":203},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/demo.sidekick.be\/itenium\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Managing Environment Variables with PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/\",\"name\":\"Itenium\",\"description\":\"We build software. We build careers.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\",\"name\":\"Usdevelop\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"caption\":\"Usdevelop\"},\"sameAs\":[\"https:\/\/demo.sidekick.be\/itenium\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Managing Environment Variables with PowerShell - Itenium","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Managing Environment Variables with PowerShell - Itenium","og_description":"Working with environment variables in Windows is as easy as: Win + Pause &gt; \"Advanced system settings\" &gt; \"Environment Variables...\" After which you get a tiny, unresizable, form where you can view and manage them. Something better eventually arrived with Windows 10 but still, PowerShell :)Use Autohotkey to open the window with Left Alt + Pause: LAlt &amp; Pause::Run % \"rundll32 sysdm.cpl,EditEnvironmentVariables\"","og_url":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/","og_site_name":"Itenium","article_published_time":"2018-08-24T20:00:00+00:00","article_modified_time":"2025-11-28T12:31:47+00:00","og_image":[{"width":360,"height":203,"url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg","type":"image\/jpeg"}],"author":"Usdevelop","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Usdevelop","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/","url":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/","name":"Managing Environment Variables with PowerShell - Itenium","isPartOf":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#website"},"primaryImageOfPage":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#primaryimage"},"image":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#primaryimage"},"thumbnailUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg","datePublished":"2018-08-24T20:00:00+00:00","dateModified":"2025-11-28T12:31:47+00:00","author":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022"},"breadcrumb":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#primaryimage","url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg","contentUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/environment-variables.jpg","width":360,"height":203},{"@type":"BreadcrumbList","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/dev-setup\/environment-variables-management-with-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/demo.sidekick.be\/itenium\/"},{"@type":"ListItem","position":2,"name":"Managing Environment Variables with PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/demo.sidekick.be\/itenium\/#website","url":"https:\/\/demo.sidekick.be\/itenium\/","name":"Itenium","description":"We build software. We build careers.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022","name":"Usdevelop","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","caption":"Usdevelop"},"sameAs":["https:\/\/demo.sidekick.be\/itenium"]}]}},"_links":{"self":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/comments?post=1703"}],"version-history":[{"count":3,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1703\/revisions"}],"predecessor-version":[{"id":2324,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1703\/revisions\/2324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media\/2122"}],"wp:attachment":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media?parent=1703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/categories?post=1703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/tags?post=1703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}