{"id":2425,"date":"2012-06-29T08:54:46","date_gmt":"2012-06-29T12:54:46","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=2425"},"modified":"2012-06-29T08:54:46","modified_gmt":"2012-06-29T12:54:46","slug":"friday-fun-expand-environmental-variables-in-powershell-strings","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/","title":{"rendered":"Friday Fun: Expand Environmental Variables in PowerShell Strings"},"content":{"rendered":"<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable-300x71.png\" alt=\"\" title=\"powershellvariable\" width=\"300\" height=\"71\" class=\"alignleft size-medium wp-image-2429\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable-300x71.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable.png 524w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>This week I was working on a project that involved using the %PATH% environmental variable. The challenge was that I have some entries that look like this: %SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\. When I try to use that path in PowerShell, it complains because it doesn't expand %SystemRoot%. What I needed was a way to replace it with the actual value, which I can find in the ENV: PSdrive, or reference as $env:systemroot. This seems reasonable enough. Take a string, use a regular expression to find the environmental variable, find the variable in ENV:, do a replacement, write the revised string back to the pipeline. So here I have Resolve-EnvVariable.<\/p>\n<p><code lang=\"Powershell\"><\/p>\n<p>Function Resolve-EnvVariable {<\/p>\n<p>[cmdletbinding()]<br \/>\nParam(<br \/>\n[Parameter(Position=0,ValueFromPipeline=$True,Mandatory=$True,<br \/>\nHelpMessage=\"Enter a string that contains an environmental variable like %WINDIR%\")]<br \/>\n[ValidateNotNullOrEmpty()]<br \/>\n[string]$String<br \/>\n)<\/p>\n<p>Begin {<br \/>\n    Write-Verbose \"Starting $($myinvocation.mycommand)\"<br \/>\n} #Begin<\/p>\n<p>Process {<br \/>\n    #if string contains a % then process it<br \/>\n    if ($string -match \"%\\S+%\") {<br \/>\n        Write-Verbose \"Resolving environmental variables in $String\"<br \/>\n        #split string into an array of values<br \/>\n        $values=$string.split(\"%\") | Where {$_}<br \/>\n        foreach ($text in $values) {<br \/>\n            #find the corresponding value in ENV:<br \/>\n            Write-Verbose \"Looking for $text\"<br \/>\n            [string]$replace=(Get-Item env:$text -erroraction \"SilentlyContinue\").Value<br \/>\n            if ($replace) {<br \/>\n                #if found append it to the new string<br \/>\n                Write-Verbose \"Found $replace\"<br \/>\n                $newstring+=$replace<br \/>\n            }<br \/>\n            else {<br \/>\n                #otherwise append the original text<br \/>\n                $newstring+=$text<br \/>\n            }<\/p>\n<p>        } #foreach value<\/p>\n<p>        Write-Verbose \"Writing revised string to the pipeline\"<br \/>\n        #write the string back to the pipeline<br \/>\n        Write-Output $NewString<br \/>\n    } #if<br \/>\n    else {<br \/>\n        #skip the string and write it back to the pipeline<br \/>\n        Write-Output $String<br \/>\n    }<br \/>\n} #Process<\/p>\n<p>End {<br \/>\n    Write-Verbose \"Ending $($myinvocation.mycommand)\"<br \/>\n} #End<br \/>\n} #end Resolve-EnvVariable<br \/>\n<\/code><\/p>\n<p>The function takes a string as a parameter, or you can pipe into the function. The function looks to see if there is something that might be an environmental variable using a regular expression match.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nif ($string -match \"%\\S+%\") {<br \/>\n<\/code><\/p>\n<p>If there is an extra % character in the string, this won't work so I'm assuming you have some control over what you provide as input. Now I need to get the match value. At first I tried using the Regex object. But when faced with a string like this \"I am %username% and working on %computername%\" it also tried to turn % and working on% as an environmental variable. I'm sure there's a regex pattern that will work but I found it just as easy to split the string on the % character and trim off the extra space.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\n$values=$string.split(\"%\") | Where {$_}<br \/>\n<\/code><\/p>\n<p>Now, I can go through each value and see if there is a corresponding environmental variable.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nforeach ($text in $values) {<br \/>\n   #find the corresponding value in ENV:<br \/>\n    Write-Verbose \"Looking for $text\"<br \/>\n   [string]$replace=(Get-Item env:$text -erroraction \"SilentlyContinue\").Value<br \/>\n<\/code><\/p>\n<p>I turned off the error pipeline to suppress errors about unfound entries. If something was found then I do a simple replace, otherwise, I re-use the original text.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nif ($replace) {<br \/>\n   #if found append it to the new string<br \/>\n   Write-Verbose \"Found $replace\"<br \/>\n   $newstring+=$replace<br \/>\n}<br \/>\nelse {<br \/>\n   #otherwise append the original text<br \/>\n   $newstring+=$text<br \/>\n}<br \/>\n<\/code><\/p>\n<p>In essence I am building a new string adding the replacement values or original text. When finished I can write the new string, which has the variable replacements back to the pipeline.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nWrite-Verbose \"Writing revised string to the pipeline\"<br \/>\n#write the string back to the pipeline<br \/>\nWrite-Output $NewString<br \/>\n<\/code><\/p>\n<p>Finally, I can pass strings that contain environmental variables to the function.<\/p>\n<p><code lang=\"DOS\"><br \/>\nPS C:\\> \"I am %username% and working on %computername%\" | resolve-envvariable<br \/>\nI am Jeff and working on SERENITY<br \/>\n<\/code><\/p>\n<p>This isn't perfect. Look what happens if there is an undefined variable:<\/p>\n<p><code lang=\"DOS\"><br \/>\nPS C:\\> \"I am %username% and working on %computername% with a %bogus% variable.\" | resolve-envvariable<br \/>\nI am Jeff and working on SERENITY with a bogus variable.<br \/>\n<\/code><\/p>\n<p>But as long as you are confident that variables are defined, then you can do things like this:<\/p>\n<p><code lang=\"DOS\"><br \/>\nPS C:\\> $env:path.split(\";\") | Resolve-EnvVariable | foreach { if (-Not (Test-Path $_)) {$_}}<br \/>\nc:\\foo<br \/>\n<\/code><\/p>\n<p>Download <a href='http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/Resolve-EnvVariable.txt' target='_blank'>Resolve-EnvVariable<\/a> and let me know what you think. The download version includes comment based help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week I was working on a project that involved using the %PATH% environmental variable. The challenge was that I have some entries that look like this: %SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\. When I try to use that path in PowerShell, it complains because it doesn&#8217;t expand %SystemRoot%. What I needed was a way to replace it with the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[72,271,4],"tags":[395,568,534,268,394],"class_list":["post-2425","post","type-post","status-publish","format-standard","hentry","category-commandline","category-friday-fun","category-powershell","tag-env","tag-friday-fun","tag-powershell","tag-regex","tag-scriptiing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Friday Fun: Expand Environmental Variables in PowerShell Strings &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Friday Fun: Expand Environmental Variables in PowerShell Strings &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"This week I was working on a project that involved using the %PATH% environmental variable. The challenge was that I have some entries that look like this: %SystemRoot%system32WindowsPowerShellv1.0. When I try to use that path in PowerShell, it complains because it doesn&#039;t expand %SystemRoot%. What I needed was a way to replace it with the...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2012-06-29T12:54:46+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable-300x71.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Friday Fun: Expand Environmental Variables in PowerShell Strings\",\"datePublished\":\"2012-06-29T12:54:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/\"},\"wordCount\":390,\"commentCount\":20,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/06\\\/powershellvariable-300x71.png\",\"keywords\":[\"env\",\"Friday Fun\",\"PowerShell\",\"regex\",\"Scriptiing\"],\"articleSection\":[\"CommandLine\",\"Friday Fun\",\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/\",\"name\":\"Friday Fun: Expand Environmental Variables in PowerShell Strings &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/06\\\/powershellvariable-300x71.png\",\"datePublished\":\"2012-06-29T12:54:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/06\\\/powershellvariable.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/06\\\/powershellvariable.png\",\"width\":\"524\",\"height\":\"125\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2425\\\/friday-fun-expand-environmental-variables-in-powershell-strings\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"CommandLine\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/commandline\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Friday Fun: Expand Environmental Variables in PowerShell Strings\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Friday Fun: Expand Environmental Variables in PowerShell Strings &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/","og_locale":"en_US","og_type":"article","og_title":"Friday Fun: Expand Environmental Variables in PowerShell Strings &#8226; The Lonely Administrator","og_description":"This week I was working on a project that involved using the %PATH% environmental variable. The challenge was that I have some entries that look like this: %SystemRoot%system32WindowsPowerShellv1.0. When I try to use that path in PowerShell, it complains because it doesn't expand %SystemRoot%. What I needed was a way to replace it with the...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/","og_site_name":"The Lonely Administrator","article_published_time":"2012-06-29T12:54:46+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable-300x71.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Friday Fun: Expand Environmental Variables in PowerShell Strings","datePublished":"2012-06-29T12:54:46+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/"},"wordCount":390,"commentCount":20,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable-300x71.png","keywords":["env","Friday Fun","PowerShell","regex","Scriptiing"],"articleSection":["CommandLine","Friday Fun","PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/","name":"Friday Fun: Expand Environmental Variables in PowerShell Strings &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable-300x71.png","datePublished":"2012-06-29T12:54:46+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/06\/powershellvariable.png","width":"524","height":"125"},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2425\/friday-fun-expand-environmental-variables-in-powershell-strings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"CommandLine","item":"https:\/\/jdhitsolutions.com\/blog\/category\/commandline\/"},{"@type":"ListItem","position":2,"name":"Friday Fun: Expand Environmental Variables in PowerShell Strings"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1801,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/1801\/finding-files-in-the-path-a-pipeline-perk\/","url_meta":{"origin":2425,"position":0},"title":"Finding Files in the Path &#8211; A Pipeline Perk","author":"Jeffery Hicks","date":"November 17, 2011","format":false,"excerpt":"I've been chipping in on a forum post about finding if a given file exists in any folder within the system environmental %PATH% variable using Windows PowerShell. There are several ways you might approach this. But the best way in my opinion is to leverage the PowerShell pipeline. Perhaps you\u2026","rel":"","context":"In &quot;Scripting&quot;","block_context":{"text":"Scripting","link":"https:\/\/jdhitsolutions.com\/blog\/category\/scripting\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6672,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6672\/going-down-the-right-path-with-powershell\/","url_meta":{"origin":2425,"position":1},"title":"Going Down the Right %PATH% with PowerShell","author":"Jeffery Hicks","date":"April 19, 2019","format":false,"excerpt":"I trust that most of you are aware that the reason it is often easy to run command and programs in Windows, especially items from the command prompt, is thanks to a system environment variable called PATH. When you tell Windows to run a command, without using the complete path\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/04\/image_thumb-17.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/04\/image_thumb-17.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/04\/image_thumb-17.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/04\/image_thumb-17.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1962,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1962\/friday-fun-whats-my-variable\/","url_meta":{"origin":2425,"position":2},"title":"Friday Fun What&#8217;s My Variable","author":"Jeffery Hicks","date":"January 6, 2012","format":false,"excerpt":"I use scriptblocks quite a bit in my PowerShell work, often saved as variables. These are handy for commands you want to run again, but don't necessarily need to turn into permanent functions. $freec={(get-wmiobject win32_logicaldisk -filter \"deviceid='c:'\" -property Freespace).FreeSpace\/1mb} Now in PowerShell I can invoke the scriptblock. PS S:\\> &$freec\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/get-variabletype-1-300x197.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1660,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1660\/friday-fun-what-a-char\/","url_meta":{"origin":2425,"position":3},"title":"Friday Fun What a CHAR!","author":"Jeffery Hicks","date":"September 23, 2011","format":false,"excerpt":"Last week I posted a PowerShell snippet on Twitter. My original post piped an array of integers as [CHAR] type using an OFS. Don't worry about that. As many people reminded me, it is much easier to use the -Join operator. -join [char[]](116,103,105,102) I'll let you try that on your\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1389,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1389\/friday-fun-randomness-abounds\/","url_meta":{"origin":2425,"position":4},"title":"Friday Fun Randomness Abounds","author":"Jeffery Hicks","date":"April 29, 2011","format":false,"excerpt":"I find it a little ironic that although I like efficiency, structure and order I'm fascinated with randomness. In today's Friday Fun I want to explore a few ways you might incorporate randomness into your PowerShell scripting. Perhaps you need a random number between 100 and 500. Or a string\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2330,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2330\/friday-fun-get-latest-powershell-scripts\/","url_meta":{"origin":2425,"position":5},"title":"Friday Fun: Get Latest PowerShell Scripts","author":"Jeffery Hicks","date":"May 18, 2012","format":false,"excerpt":"Probably like many of you I keep almost all of my scripts in a single location. I'm also usually working on multiple items at the same time. Some times I have difficult remembering the name of a script I might have been working on a few days ago that I\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/get-latestscript-300x133.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2425"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2425\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}