Use PowerShell to get the local computer name

Up until recently, ever time I wanted to use the local computer’s name in a PowerShell script I would use the following line of code:

$(Get-WmiObject Win32_Computersystem).name

It does the job, but it’s cumbersome, and lately I’ve been thinking that there had to be a better way.  This is PowerShell after all.  Well, after a little digging it turns out that there is in fact a better way (IMHO).  Simply reference the following environmental variable, which is automatically recognized in PowerShell:


That’s a difference of 25 characters, and is certainly a lot easier to recall and type out.  So, it occurred to me that I should see what additional environmental variables I could access this way.  As it turns out, I can get all of them this way.  By using the following line of code…

get-childitem env:

… I get a full listing of all my environmental variables.  Simply slap the name property on after “$env:” and you’ve got your variable.  For instance, if I quickly want to reference the System32 folder in my script I can simply type:

$env: homepath

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: