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. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.