Aug 212013
 
 August 21, 2013  Posted by at 7:52 am Not So Stupid Questions, PowerShell  Add comments

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

If you want to save the path of an executable to a permanent variable you can do so from Command Prompt or PowerShell. Setting one in Command Prompt does not set it in PowerShell. Environment variables are values stored in an object (and can be changed) and these are stored in the registry. (Stupid Question 114: What is the “Windows Registry”?).

Variable in registry

Variable in registry

The syntax for setting one temporarily is by using
set variableName C:\mypath\myExecutable.exe
To set it permanently use
setx variableName C:\mypath\myExecutable.exe

To read the contents:
echo %variableName%

To invoke/use variable:
%variableName%

In PowerShell they are set the same way,
Setx variableName C:\mypath\myExecutable.exe

But you read it by using:
gi env:variableName

And invoke/use by:
& $env:variableName

Give it a go with the C# compiler, by using (this is the path for me on my Windows 8 computer for the csc.exe) in PowerShell:
setx csc C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

Then compile a .cs file like so:

C:\Temp\ & $env:csc test.cs

This outputs test.exe

If you prefer clicking on buttons you can set variables in Advanced System Settings:

'Cheating' by setting variables in Advanced System Settings ;)

‘Cheating’ by setting variables in Advanced System Settings 😉

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 13 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)