Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Why does $ENV | GET-MEMBER not work?

Tags:

powershell

I come from a C, C++, C#, python background so i'm applying this thought pattern to Powershell which i'm learning from scratch but I'm a little confused so far as at first glance it seems to be inconsistent and does not follow a fixed base class type structure for all objects so that things can be queried in a consistent manner.

The following works fine:

   $host | get-member
   $env:username

So $env is a valid object but this does not work:

   $env | get-member

These also do not work:

   $env.gettype()
   dir $env
   dir $env:

but this type query on $host does so I'm assuming $host is a .net variable but $env is not?

   $host.gettype()

I found that env: also works with dir (aka get-childitem) but this colon is yet another type of notation i'm unfamiliar with and things are starting to get very confusing now. This does not seem to be a string format in this case which I have seen some google posts about so what is it? It behaves like a member selection or dictionary key specifier. If it is a member selector or dictionary key then i would expect get-member to work because it is a standard object.

This outputs the variables and values that I wanted but I don't understand why this syntax is used. This is not DOS syntax either so what's going on here?

   dir env:

But dir $env seems to equate to dir $env:userprofile???? why?

Therefore $host appears to be a .net object but $env or env: is something else completely different and I've no idea what object type it is in the grand scheme of things and cannot seem to query it's type with by conventional means. Initial thoughts are that it is a list object of sorts because get-childitem works with it but other than that I'm completely lost.

I'm clearly missing something here so can someone steer me in the right direction please?

like image 372
DelboyJay Avatar asked Sep 24 '26 06:09

DelboyJay


1 Answers

Get-Help 'about_Providers' -ShowWindow shows that env: is drive in Environment Provider, i.e. one of Windows PowerShell providers.

BUILT-IN PROVIDERS: Windows PowerShell includes a set of built-in providers that you can use to access the different types of data stores.

Provider      Drive         Data store
--------      -----         ----------
Alias         Alias:        Windows PowerShell aliases
Certificate   Cert:         x509 certificates for digital signatures
Environment   Env:          Windows environment variables
FileSystem    *             File system drives, directories, and files
Function      Function:     Windows PowerShell functions
Registry      HKLM:, HKCU:  Windows registry
Variable      Variable:     Windows PowerShell variables
WSMan         WSMan:        WS-Management configuration information

* The FileSystem drives vary on each system. 

You can also create your own Windows PowerShell providers, and you can install providers that others develop. To list the providers that are available in your session, type: get-psprovider.

That's why Get-ChildItem env: works in contrary to dir $env:, dir $env etc.

like image 168
JosefZ Avatar answered Sep 27 '26 01:09

JosefZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!