Comments (5)
The intent documentation is to document the behavior of PowerShell, not the .NET API. Also, the behavior gets a little more complicated when you compare Windows to non-Windows platforms.
Windows, normally, doesn't allow you to create an environment variable with a null or empty value. You can't do it using the set
command in cmd.exe
, nor can you do it in the control panel applet that manages environment variables. Also, the set
command can't see the null-valued environment variable. It's odd that setx.exe
allows you to do it.
C:\>set TEST
Environment variable TEST not defined
C:\>pwsh -noprofile
PowerShell 7.4.1
PS C:\> [System.Environment]::GetEnvironmentVariables([EnvironmentVariableTarget]::User).ContainsKey('TEST')
True
In bash on Linux or zsh on macOS, you can create and export environment variable with a null or empty value.
In all platforms, the PowerShell environment provider is using the same underlying .NET API to get the list of environment variables. On Windows, that API does not list the null-valued variables. On non-Windows, the API includes the null-valued variables. Windows hides the null-valued variables.
With all that said, I think we can update the docs I will reopen and amend your PR with editorial changes.
from powershell-docs.
See my comment in the PR - #10851 (comment)
from powershell-docs.
The [System.Environment]::GetEnvironmentVariables()
method can see the environment variable A
, but the PowerShell environment provider can't.
from powershell-docs.
@sdwheeler Thanks for taking the time to look at this! I experienced a lot of frustration when I took this documentation as fact in the way it is currently written, and I'm definitely hoping to help set it right.
For what it's worth, [System.Environment]::GetEnvironmentVariables()
showing the empty environment variable isn't an outlier - it's actually more compatible with other apps than the PowerShell Environment provider is. π€¦ββοΈ
The documentation stating broadly that all environment variables cannot be blank - regardless how they are consumed (such as by nested processes, as is common in a shell) or produced (such as by parent processes, as in common in nested shells or in software development scenarios), and without an aside stating PowerShell is unusual for this behavior - is incorrectly misleading about all of the environments PowerShell is supported for.
from powershell-docs.
Just a few clarifications:
-
setx.exe
doesn't actually work with empty values:- Even though it should report an error, it blindly creates an empty
REG_SZ
registry value, which the system ignores when creating the environment block for a process - and the process-level environment variables are what matter (in most cases). - As a probably accidental side effect of how .NET implements lookup of the persisted environment-variable definitions in the registry, it does surface such broken entries; if you omit the
[EnvironmentVariableTarget]::User
part or specify[EnvironmentVariableTarget]::Process
, you'll see that variable is not found, i.e. that the process doesn't see it.
- Even though it should report an error, it blindly creates an empty
-
The upshot is that on Windows the persistent definition of empty environment variables is effectively not supported.
-
However, the in-process definition of empty environment variables is de facto supported via the Windows API, but this capability - whose intentionality is unclear - is currently not surfaced via .NET APIs, not even on Unix-like platforms, where empty environment variables aren't uncommon, and easily creatable in-process from POSIX-compatible shells.
- See dotnet/runtime#50554, also for a discussion of why this is problematic, including especially for PowerShell.
A PowerShell example that demonstrates that creating empty-value env. vars. in-process is possible on Windows:
Add-Type -Namespace Demo -Name WinApiHelper -MemberDefinition @'
[DllImport("kernel32", CharSet = CharSet.Unicode)]
public static extern bool SetEnvironmentVariable(string lpName, string lpValue);
'@
if ([Demo.WinApiHelper]::SetEnvironmentVariable('NOVALUE', '')) {
'Successfully created $env:NOVALUE with no value (empty string).'
} else {
throw 'Failed to create $env:NOVALUE with no value (empty string).'
}
# Test the existence of the variable.
Get-Item Env:NOVALUE
from powershell-docs.
Related Issues (20)
- libssl1.1 is not longer included in alpine 3.19 HOT 1
- Inadequate explanation of terminology HOT 3
- Clarify that `-IsValid` requires a drive being referred to to exist, and that 7+ no longer performs syntax validation HOT 1
- Update `Get-Service` doc on behavior
- Add example of specifying provider for `Test-Path -IsValid` HOT 1
- Typo in the "Γrnek 4" section HOT 1
- Example for "Example 1: Suspend a workflow job by name" missing a linebreak
- Ubuntu-based distribution have arm64 available HOT 2
- -InformationVariable common parameter description has missing data
- Extradebot
- We need docs about using of Windows Runtime assemblies HOT 2
- update
- How to extract all data parameters from an event log HOT 1
- $PSCmdlet.ParameterSetName not covered HOT 1
- redacted
- SSL certificate automation power shell script for windows server and websites or related docs HOT 1
- Highlight the change in underlying .Net version may have new method overloads
- No required configuration foa active directory HOT 1
- Clarify that a [bigint] operand in an arithmetic operation forces the other operand to [bigint] too
- An incorrect reference to $DebugPreference variable values
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell-docs.