The environment provider has numerous built‑in variables that stores
information about the operating system environment.
These information are accessed by OS, scripts, and programs.
These variables provide information like the Windows directory,
OS installation path, temporary directory path,
user's OneDrive, and many such details, which we will look further in demo.
Almost all these variables are self‑explanatory,
and we won't feel the need to emphasize much on each
and every environment variable.
We'll try to cover most of them and especially the ones that are used widely.
So now, we will quickly jump into our demo environment.
In this demo, we will look into the significance of environment variables.
We will then see how to extract values out of these environment variables.
To list all the environment variables.
I will run Get‑ChildItem and enter env:\ drive.
On the left side, we have the list of environment variables,
and on the right side, we have the values that are stored in those variables.
The first one is ALLUSERSPROFILE that points to c:\ProgramData folder.
The second one is APPDATA that points to Users, Roaming folder.
So, if in a script, I want to change my directory to this path,
I can easily mention ALLUSERSPROFILE variable instead of
typing the entire Roaming folder path.
We then have ProgramFiles variable that provides
details about ProgramFiles folders.
Next is one of the widely used variable COMPUTERNAME. That
provides the hostname of the system.
Imagine you are extracting some data from hundreds of computers and
exporting their data in their respective text file.
If you bring all those files together,
how will you differentiate between each file?
The solution to this is that you can use COMPUTERNAME variable in the script,
extract the hostname, and save the text file with the hostname in it.
This would look something like this.
The ComSpec variable specifies and points to the
command‑line interpreter of the OS.
DriverData variable gives information about DriverData folder path.
The HOMEDRIVE, HOMEPATH,
and LOCALAPPDATA variables points to the Users home drive,
Users home directory, and AppData\Local folder.
We then have LOGONSERVER variable that basically tells us which
domain controller the system has logged on to.
In case of non‑domain joined machine or local user login,
local computers name will be shown here.
We then have some variables that gives information about processor.
The OneDrive variable specifies the path to Users OneDrive location.
The Path variable specifies group of directories where
executable programs are located.
PSModulePath specifies the paths where PowerShell modules resides.
Then we have the variable for PUBLIC folder,
SystemDrive, SystemRoot, and Temp folders.
We then have variables that provides information about Users domain,
Users account, and Users profile.
Lastly,
we have windir variable that specifies the drive where Windows is installed.
I'll now show you how to extract values from these variables.
The syntax to use environment variable is to start with
dollar sign and then and enter env:colon,
followed by variable name, First, I'll take an example of COMPUTERNAME variable.
When I hit Enter, it will return COMPUTERNAME.
The example which we saw earlier where COMPUTERNAME was served
in the file can be achieved by doing this.
In the FilePath parameter of Out‑File command, once you enter the FileName,
you can append the COMPUTERNAME.
This will extract hostname from COMPUTERNAME environment
variable and append it to the FileName.
I'll now show you the file that is created. Here,
it has the FileName, followed by the hostname.
Similarly,
I can use USERPROFILE variable to save the file directly in the Users directory.
I can fetch my Log‑on server using LOGONSERVER variable. I
can also get the details of my Active Directory domain using
USERDNSDOMAIN variable; likewise,
you can make use of these env variables in a number of places, and that totally depends on your script requirement.