I was using tab autocompletion for a variable name in Powershell 5.1 today and noticed that one of the choices was the name of a PSDrive. The drive name is docs
and I wanted to expand is called $document_name
. When I typed $do<tab>
, the shell did indeed expand what I had typed to $document_name
but for some reason, I typed <tab>
a second time and that's when the expanded text changed to $docs:
.
I explored further and found that this type of variable exists for each of my PSDrives, or at least tab expansion suggests that it does.
More formally, for every PSDrive PSD, tab expansion believes that $PSD:
is a valid thing.
My question is simple: what the heck are these? Here are some observations I've made so far:
- These names are prefixed with
$
, so they look like PS variables. For the rest of this discussion (and in the earlier discussion above), I will assume they are variables and refer to them as such.
- Although they appear to be variables, they are not listed in the
Variable:
PSDrive like most variables. In this way, it behaves like the $env
"variable," which also is not listed in Variable:
. I have a feeling if I could find documentation about $env
, then I'd understand these objects also.
- In some ways, they behave like pointers to filesystem objects. For example, if there is a file name
readme.txt
containing the text "Hello, world!" on a PSDrive named code
, then all of the following are possible interactions with Powershell.
Fetch the contents of the file.
λ ${code:
eadme.txt}
Hello, world!
Just to prove that the type of the above result is String
:
λ ${code:
eadme.txt} | % { $_.GetType().Name }
String
Trying to use this as a reference to the PSDrive doesn't work well for many operations, such as cd
:
C:
λ cd ${code:}
At line:1 char:4
+ cd ${code:}
+ ~~~~~~~~
Variable reference is not valid. The variable name is missing.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidBracedVariableReference
I could go on, but I'm stumped. If I pass $code:
(or $env:
, for that matter) to Get-Member
, I get an error saying Variable reference is not valid
.
So just what the heck are "variables" like $env
and $<PSDrive>:
(such as $code:
)? Are they expressions? Built-in expressions? Some kind of object? Thanks for any help.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…