After setting a variable in a session it cant be retrieved with Get-Variable

I understand that we can setup an env variable as follows:

$env:SmithySettings_ScriptTokens__ReleaseVersion = “2.4.0-ci.99”

I ran my app command and it pickec up the right value. However, when I tried to unset and removing it using

Remove-Variable -name SmithySettings_ScriptTokens__ReleaseVersion

I get

Remove-Variable: Cannot find a variable with the name ‘SmithySettings_ScriptTokens__ReleaseVersion’

I tried to print all session variables with Get-Variable, it is not there where as when I ran again my app it picked up the right value from the variable. Is there anything wrong I am doing? Thanks for your help

Use “Remove-Item”.
How to Remove Environment Variables in PowerShell?

I did but remove-item does not recognize the environment variable, however, doing

Get-ChildItem Env:

I see my variable in the list. I tried both

Remove-Item SmithySettings_ScriptTokens__ReleaseVersion

Remove-Item $env:SmithySettings_ScriptTokens__ReleaseVersion

I get

Remove-Item: Cannot find path 'C:\Projects\WindowsScripts\PS\Scripts\Smithy…

The error is correct . . . your variable isn’t a file. The Remove-Item cmdlet works with many “providers” (file system, registry, etc.). You haven’t specified a provider so the cmdlet assumes a default and looks for a file system object. In your case it’s a file named “SmithySettings_ScriptTokens__ReleaseVersion” in the current directory.

Your second attempt (using a variable named $env) is closer, but still incorrect. The provider name should be “ENV:”. The “$env:” is a variable prefix, not a provider.

Remove-Item ENV:SmithySettings_ScriptTokens__ReleaseVersion

So, ENV:some_name is a PATH to an environment variable, while $env:some_name is the NAME of an environment variable.

Yes this worked fine so many thanks for your help

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.