Skip to content

Seemingly-obscure ${<file path>} feature broken on Linux #25760

Open

Description

@helpimnotdrowning

Prerequisites

(I must note that searching the Issues page for "${" is either not possible or very difficult since there are no results, sorry if this happens to be a dupe!)

While reading the PowerShell Wikipedia page, I cane across a sentence that described a feature I have never seen documented:

Enclosing the path to a file in braces preceded by a dollar sign (as in ${C:\foo.txt}) creates a reference to the contents of the file. If it is used as an L-value, anything assigned to it will be written to the file. When used as an R-value, the contents of the file will be read. If an object is assigned, it is serialized before being stored.[citation needed]

This works on both Windows PowerShell and Core, but not on Core for Linux.

Steps to reproduce

On a Linux system, write

${/etc/profile} # (or any other file that exists)

Expected behavior

PS> ${/etc/profile}
< contents of /etc/profile here... >

Actual behavior

PS> ${/etc/profile}
< no output >

Error details

n/a

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.1
PSEdition                      Core
GitCommitId                    7.5.1
OS                             Debian GNU/Linux 12 (bookworm)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Jul 16, 2025
  2. mklement0 commented on Jul 16, 2025

    @mklement0
    Contributor

    Good point; the feature name is namespace variable notation, and the issue was first raised in 2017:

    [Update: ${/etc/profile} isn't actually an instance of namespace variable notation, but ${/:/etc/profile} would be; see below for a summary.]

    The only workaround is to define a helper PS drive with, say, New-PSDrive RootDir FileSystem /, and then use that drive as the root, e.g. ${RootDir:/etc/profile}

    That said, the usefulness of namespace variable notation with respect to file-system paths (the FileSystem provider) has always been limited by the need to use literal paths, which may explain why the feature was never fixed on Unix-like platforms.

  3. 237dmitry commented on Jul 16, 2025

    @237dmitry

    New-PSDrive RootDir FileSystem /

    Why / PSDrive is not working? But ${Temp:/file} is.

    $ Get-PSDrive /
    Name           Used (GB)     Free (GB) Provider      Root
    ----           ---------     --------- --------      ----
    /                  19,95        213,44 FileSystem    /   
    
  4. mklement0 commented on Jul 16, 2025

    @mklement0
    Contributor

    Indeed, 237dmitry: by the logic of namespace variable notation, ${/:/etc/profile} should work, as mentioned in #3245 and as argued in detail in #2268 (comment) (a PowerShell drive spec is needed, and / is indeed the name of a PowerShell drive on Unix-like platforms, as you show).

  5. 237dmitry commented on Jul 16, 2025

    @237dmitry

    ${/:/etc/profile} should work

    Of course, I tried it first.

  6. mklement0 commented on Jul 16, 2025

    @mklement0
  7. 237dmitry commented on Jul 16, 2025

    @237dmitry

    Your follow-up comment adds nothing to this discussion

    Your comment nothing explained. It is important to understand why the same syntax does not work for different PSDrives

    PS > ${Temp:/a.txt} = "aaaa"
    PS > ${Temp:/a.txt} += "`nbbbb"
    PS > ${Temp:/a.txt}
    aaaa
    bbbb
    
    PS > ${/:/etc/environment}     # nothing
    PS > gc /etc/environment
    BROWSER=firefox
    EDITOR=mcedit
    VISUAL=mcedit
    POWERSHELL_TELEMETRY_OPTOUT=1
    POWERSHELL_UPDATECHECK=Off
    PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/opt/bin
    SUDO_EDITOR=mcedit
    COLORTERM=truecolor
    
  8. mklement0 commented on Jul 16, 2025

    @mklement0
  9. helpimnotdrowning commented on Jul 16, 2025

    @helpimnotdrowning
    Author

    237dmitry,

    New-PSDrive RootDir FileSystem /

    Why / PSDrive is not working? But ${Temp:/file} is.

    if I may add not to argue, but New-PSDrive RootDir FileSystem / created a drive called RootDir:, not /:, so you would have to do ${RootDir:/etc/environment}, not ${/:/etc/environment}. Doing Get-PSDrive / gets the drive for the "main" PSDrive, which is used unqualified (no :). / can't be used for a PSDrive name:

    new-psDrive -name '/' -PSProvider FileSystem -Root '/'
    New-PSDrive: Cannot process the drive name because the drive name contains one or more of the following characters that are not valid: ; ~ / \ . :

    edit: i think the reason this doesn't work is that Windows drive names like C: share the PS syntax for namespacing variables, but Unix systems use a single "drive" at /, which means that it does not get interpreted as a namespace variable to a filesystem, but instead as a literal variable name:

    > ${/etc/somewhere} = 1
    > get-variable
    
    Name                           Value
    ----                           -----
    # < ... >
    ?                              True
    /etc/somewhere                 1
    ^                              ${/etc/somewhere}
    # < ... >
  10. 237dmitry commented on Jul 16, 2025

    @237dmitry

    because the drive name contains one or more of the following characters that are not valid

    This is the good explanation, thank you.

  11. mklement0 commented on Jul 16, 2025

    @mklement0
  12. mklement0 commented on Jul 17, 2025

    @mklement0
    Contributor

    Let me attempt a summary:

    • The semi-official name of the feature in question is namespace variable notation.

    • Variable assigment to files ignores all encoding settings and uses ... ASCII? #2268 (comment) provides an overview of the feature and its (dearth of) documentation.

    • It follows from the description of the feature in the linked comment that the example used in the initial post, ${/etc/profile} is not an instance of namespace variable notation (it refers to a regular variable with the unusual name /etc/profile), because it lacks a PowerShell drive specification (e.g., C: on Windows, or Temp: on both Windows and Unix), i.e. a reference to a PowerShell drive (which on Windows includes native file-system drives) with an underlying provider that implements the System.Management.Automation.Provider.IContentCmdletProvider interface.

    • However, namespace variable notation is indeed broken on Unix-like platforms, as previously discussed in issues such as Namespace variable notation referencing the FileSystem provider is broken on Unix platforms #3245:

      • While / is the name of a built-in PowerShell drive on Unix-like platforms (referring to the file-system root), it cannot be used as such, because /: isn't recognized as a drive spec.: ${/:/etc/profile} fails, even though it should report the contents of /etc/profile; similarly, trying to reference a file with a relative path, relative to the current location of said drive, doesn't work either, e.g. ${/:file.txt}

        • The fact that PowerShell - sensibly - prevents defining custom drives with a name containing / shouldn't prevent referring to the special, built-in / drive by way of a drive spec. (/:)
      • A - cumbersome - workaround is to define a custom drive with a regular name, e.g. New-PSDrive RootDir FileSystem /, and use its name, e.g. ${RootDir:/etc/profile}

        • This workaround comes with a pitfall with respect to relative paths: Each PowerShell drive has its own current location, so a reference such as ${RootDrive:file.txt} would implicitly refer to a file file.txt in whatever the current location is on the RootDrive: drive, which is likely to differ from the one on the / "drive".
    • Given that this problem has been known since 2017 and no action was taken, none may ever be, possibly owing to the general limitations of namespace variable notation that are most noticeable with respect to the FileSystem provider:

      • Notable limitations are: support for literally specified paths only (even though they're interpreted as wildcard expressions), lack of control over the character encoding, references to non-existent or empty files evaluate to $null (whereas an otherwise equivalent Get-Content call yields the enumerable null, [System.Management.Automation.Internal.AutomationNull]::Value, which behaves differently as pipeline input and as input to a switch statement)
    • As for potential fixes:

      • Allowing /: as a drive spec in paths would fix the problem.

      • Alternatively - should that be considered too problematic (/: is technically a valid path on typical Unix file-systems, but then again so is function:, for instance) - or possibly complementarily, prefixing a file-system path with FileSystem:: (or, more fully, Microsoft.PowerShell.Core\FileSystem::) could be allowed in the context of namespace variable notation, e.g. ${FileSystem::/etc/profile}:

        • Notably, something like Get-Content FileSystem::/etc/profile already works.
        • Even Windows could benefit from this change, namely in the context of relative paths: currently, you must at least specify the target drive in order to use a path relative to that drive's current location, e.g. ${C:file.txt}. With the change implemented, you could more flexibly use ${FileSystem::file.txt} to target the current location of whatever drive PowerShell's current (file-system) location happens to be on.
    • Either way, the broken support on Unix-like platforms should at least be documented, though to do so meaningfully would first require documenting the feature itself properly, along the lines of creating a new conceptual help topic named, say, about_Namespace_Variable_Notation to be mentioned in and linked from about_Providers.

  13. added
    WG-NeedsReview
    WG-Engine
    on Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageWG-EngineWG-NeedsReview

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions