Thursday, February 9, 2012

Accessing Windows Environment Variables using PowerShell.

 

Hi,

Before working on script i always thought that these windows “Environment Variables” are not so important, but as i start working on PowerShell and start working on script i realise that these windows “Environment Variables” are very useful. I uses these windows “Environment Variables” in most of my scripts.

Why these windows “Environment Variables” are important?

For example you are using a script which create a SYSTEM REPORT which contain username, computer name, and Logon server, so where you will find these details ? exactly in windows “Environment Variables.  In another example , i am have a script which sent an email to "someone" but it also send a BCC email to sender, so how to access sender email ID ? i know that in windows “Environment Variables doesn't contain the any email-id, but we can create it using some trick using windows “Environment Variables.

Enough theory, Lets Start!!

How to access all windows “Environment Variables.?

In PowerShell accessing of windows “Environment Variables is very simple. the windows “Environment Variables are mounted as PsDrive in PowerShell. You just need to do run DIR on Env:\, like this  "Dir env:\" .

And this will show you all “Environment Variables which you have on your system.

09-02-2012 12-10-25 

How to access the value of “Environment Variables" using PowerShell?.

Actually there are many ways to access the values of windows "Environment Variables", i mostly used two ways to access these values and the result is same in both ways.

using Get-Item

We can use Get-Item to get the value of desired "Environment Variable".  Run the below command.

Get-Item env:\USERNAME

In the output it showing the us the value of USERNAME environment variable but along with value it also showing the "Name" in output. Let omit it.

09-02-2012 12-17-55

(Get-Item env:\USERNAME).value

Enclose the whole command in bracket after closing the bracket we are choosing to show .value properties. and you can seen below it showing us only the value of USERNAME environment variable .

09-02-2012 12-23-18

Using $ENV:

Actually this is the easy method to access the value of windows environment variables. just use $Env: and after colon provide the environment variable whose value you want to see.

$Env:username

09-02-2012 12-28-28

Simple :) isn't.

Usage {at least for me}

Example 1:

I construct email id using Username environment variable. I know in my "Foo" company every one is having there username@foo.net email id. so i construct email id like this

$email = $env:username + '@foo.net'

09-02-2012 12-37-26 

Example 2:

In some of my script i used this trick, So that i know which user is logged on which laptop and at what time.

Write-host "The user `"$env:username`" logged in to laptop  `"$env:computername`" on $(Get-Date)"  -ForegroundColor Yellow

09-02-2012 12-41-25

Video

Thanks for reading ..

Aman Dhally

messenger_freak_adp1 (30)

5 comments:

  1. Thanks Aman,
    How can I get a variable that I have defined
    like PS_My_Process_Running = 'True'

    ReplyDelete
    Replies
    1. Hi Aleem,
      thanks that u liked the blog,

      try this $PS_My_Process_Running = $true

      and you can also joined our facebook Group to stay up tp date :) http://www.facebook.com/groups/254997707860848/

      thanks
      aman

      Delete
  2. I can't figure out how to use an environment variable in a path to execute a binary. In cmd.exe I can just do %GOPATH%\bin\hello and it executes my go program
    In powershell I do $env:GOPATH + '\bin\hello' or hello.exe doesn't matter and it just prints the path at the bottom.

    ReplyDelete
    Replies
    1. Hi Nova
      for Example there is a exe name as Passgen.exe is on your desktop. and you want execute is using Environment. The try this

      & $env:USERPROFILE\desktop\passgen.exe

      make sure you add & in the front of above

      thanks
      aman

      Delete
  3. Hey Aman,

    I want to create a Environment Variable JDBC_HOME, what cmdlet i have to use for creating\adding a Environment Variable

    ReplyDelete

Note: Only a member of this blog may post a comment.