Powershell and the Environment Provider

 

The Powershell Environment Provider enables the management of the Windows environment variables by using the cmdlets normally used for file management.

On Powershell version 5.1 the Environment Provider is installed by default - using the command

Get-PSProvider

will show if it is installed on other versions.

If it is installed, the command

Get-PSDrive

will show the drive name - Env - and the command

Set-Location -Path Env:\

allows Powershell to use this drive as the root location for management of the environment variables.

 

Available cmdlets

The Environment Provider only supports a subset of all the file management cmdlets, not all of them - according to Microsoft documentation the following cmdlets are supported -

Get-Location

Set-Location

Get-Item

New-Item

Remove-Item

Clear-Item

However it appears that some other cmdlets also work - at least on Powershell version 5.1 they do.

 

Listing the environment variables

One way to see all the environment variables is by Tab Completion - Enter the command

Env:\

then repeatedly hit the Tab key - and you will see all the environment variables one by one.

 

Using cmdlets

The command

Get-Item -Path .

shows all the environment variables and their values, but not in alphabetical order.

The commands

Get-ChildItem -Path .

or

Get-ChildItem -Path Env:

show all the environment variables and their values, sorted in alphabetical order.

In addition, the command

Get-ChildItem | sort-object -property name

will also show all the environment variables and their values, sorted in alphabetical order.

The commands

Get-Item -Path PSModulePath

or

Get-ChildItem -Path PSModulePath

show only the name and value of the PSModulePath environment variable.

 

Seeing the whole string

There is a problem with two of the environment variables - ie, Path and PSModulePath - in that because they are long strings they don`t fit into the width of the Powershell window.

To see the whole of these strings there are a number of commands that show the whole string, wrapped to the window.

Get-Content PSModulePath

shows the whole value of the PSModulePath environment variable wrapped to the Powershell window.

Another command line is

(Get-Item -Path Env:PSModulePath).Value

which also shows the whole value of the PSModulePath environment variable wrapped to the Powershell window.

 

Splitting the path

These long strings with multiple paths separated by semicolons are quite hard to read - they become easier to read if they are split up into multiple lines.

Using the Split operator, the command line

(Get-Item -Path Env:PSModulePath).Value -Split ';'

shows the whole value of the environment variable split into separate lines at the semicolons.

Using the Split method, the command line

(Get-Item -Path Env:PSModulePath).Value.Split(';')

also shows the whole value of the environment variable split into separate lines at the semicolons.

Maybe worth noting that when the splitting is done, the semicolon is not shown on the print-out on the screen.

If you want to show it then when using the Split operator, the command line

(Get-Item -Path Env:PSModulePath).Value -Split '(;)'

will show the semicolons on separate lines.

 

Using variable syntax

Another way of seeing the whole string in the PSModulePath variable is to use variable syntax instead of using Powershell cmdlets.

The command line

$env:PSModulePath

will show the whole of the path string wrapped to the Powershell window.

You can also use the Split operator or the Split() method - so the commands

$env:PSModulePath -Split ';'

or

$env:PSModulePath.Split(';')

will show the whole path split across separate lines.

And as above, the command line

$env:PSModulePath -Split '(;)'

will show the semicolons.

 

Strings and Objects

It appears that there is a difference between the output of the commands involving the Get-Item / Get-ChildItem cmdlets and the commands involving the $Env:PSModulePath - even though they look the same on the screen.

This becomes significant if you are doing more than just looking at the paths on the screen - it allows for the addition of a new path to the existing path in the PSModulePath environment variable using command lines such as

Set-Item -Path Env:PSModulePath -Value ($Env:PSModulePath + ";<new-path>")

or

$Env:PSModulePath = $Env:PSModulePath+";<new-path>"

This type of addition to the path is temporary - it exists only for the current Powershell session.

And of course all the above will also work on the Path environment variable.

 

 

 

 

 

website design by ron-t

 

website hosting by freevirtualservers.com

 

© 2024   Ron Turner

 

+                                   +  

 

Link to the W3C website.   Link to the W3C website.