Home > PowerShell > Using the System.Environment Class in PowerShell

Using the System.Environment Class in PowerShell

The Environment class has a huge array of information and accessing this information is very simple, I’m going to try and give a whole bunch of one line examples of how to do this using PowerShell.

To retrieve the comptuer name

1
[System.Environment]::MachineName

Get Operating System Information (such as Platform, service pack, version, and version string)

1
[System.Environment]::OSVersion

Checking to see if your computer has started to shutdown

1
[System.Environment]::HasShutdownStarted

Instead of trying to remember if you should use `r`n or is it just `n? A simple way is just to get .NET to tell you and assign this value to a variable.

1
$nl = [System.Environment]::NewLine

Get the system directory (normally this is C:\Windows\System32), but for some computers it could be different

1
[System.Environment]::SystemDirectory

Get the user’s domain name

1
[System.Environment]::UserDomainName

Get the user’s name

1
[System.Environment]::UserName

Get a list of all environment variables

1
[System.Environment]::GetEnvironmentVariables()

Get a particular environment variable

1
2
3
4
$path = [System.Environment]::GetEnvironmentVariable("path")
$temp = [System.Environment]::GetEnvironmentVariable("temp")
$os = [System.Environment]::GetEnvironmentVariable("os")
$homedrive = [System.Environment]::GetEnvironmentVariable("homedrive")

Create a new environment variable, or overwrite an existing one

1
[System.Environment]::SetEnvironmentVariable("VariableName","VariableValue")

Get all logical drives

1
[System.Environment]::GetLogicalDrives()
  1. April 9th, 2022 at 15:07 | #1

    This blog was… how do you say it? Relevant!! Finally I have found something that helped me.
    Thanks a lot!

  2. April 9th, 2022 at 15:12 | #2

    Wonderful beat ! I would like to apprentice at the same time as
    you amend your site, how can i subscribe for a weblog web site?
    The account helped me a applicable deal. I have been tiny bit familiar
    of this your broadcast offered vibrant transparent concept

  3. April 9th, 2022 at 15:12 | #3

    I’m now not certain where you’re getting your information, however
    great topic. I needs to spend a while studying more or
    understanding more. Thanks for great information I was searching for this
    info for my mission.

  1. September 19th, 2020 at 20:40 | #1
  2. September 25th, 2020 at 03:10 | #2
  3. December 27th, 2021 at 11:29 | #3
  4. December 30th, 2021 at 07:57 | #4
  5. July 20th, 2022 at 18:48 | #5