Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

SQL Server – Windows Environment Variables and Powershell

29 August,2012 by Tom Collins

Powershell allows you to access the Windows Environment Variables. This is very useful in collecting system information . I’ve applied this technique in  creating an inventory report or troubleshooting a server issue. The purpose of Environment variables is to set values  at the OS level – that will impact processes. Accessing and reporting on the variables quickly can assist in maintaining server uptime.

A straightforward way of accessing all the environment variables is to use  “Dir env:\”  . A full list of environment variables will appear.  Although the full list appears , it requires some string manipulation to isolate one variable and value. Powershell allows you to access the individual Environment variables .

When scripting , there is a requirement to access the environment variables individually. To access individually use  $env  followed by the Windows Environment variable . The example includes the common Environment Variables, although use     “Dir env:\” to view the full list of variables

$env:COMPUTERNAME
$env:NUMBER_OF_PROCESSORS
$env:OS
$env:PROCESSOR_ARCHITECTURE
$env:PROCESSOR_IDENTIFIER
$env:PROCESSOR_LEVEL
$env:PROCESSOR_REVISION
$env:SystemDrive
$env:SystemRoot
$env:USERDNSDOMAIN
$env:USERDOMAIN
$env:USERNAME
$env:USERPROFILE
$env:LOGONSERVER
$env:HOMEDRIVE
$env:SESSIONNAME
$env:windir

 Related Posts

Powershell Scripts for DBA

Powershell - run script on all sql servers

SQL Server Database Size and Free Disk Space Daily Report


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on SQL Server – Windows Environment Variables and Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer