How to Print Environment Variables in Windows PowerShell

  1. What Is an Environment Variable?
  2. Using the PowerShell Environment Variable
  3. Printing All Environment Variables Using the Get-ChildItem Command
  4. Conclusion
How to Print Environment Variables in Windows PowerShell

In this detailed exploration, we delve into the intricate world of environment variables within Windows PowerShell, a powerful tool in any developer or system administrator’s arsenal. Environment variables are more than just settings; they are the backbone of how our operating system and applications communicate and operate efficiently.

Understanding and manipulating these variables can significantly impact our system’s behavior and our command-line proficiency.

Throughout this article, we will not only introduce the concept of environment variables in PowerShell but also demonstrate practical ways to interact with them. From retrieving specific variables to exporting a comprehensive list, our journey will equip us with the skills to effectively manage and utilize these critical system components.

What Is an Environment Variable?

Environment variables in Windows PowerShell represented as Env: play a pivotal role in storing information that affects how various programs and the operating system itself operate. These variables encompass a wide array of data, such as the operating system’s path, the location of the Windows installation directory, and the number of processes currently being utilized by the operating system, among others.

Let’s explore how we can interact with these environment variables in PowerShell.

Using the PowerShell Environment Variable

To retrieve a specific environment variable in PowerShell, we utilize the $Env: variable followed by the name of the variable we wish to access.

Consider the PATH environment variable, an essential system environment variable that enables the operating system to locate executable files from the command-line interface. To view the current PATH settings, we use the following command.

Example Code:

$env:PATH

We initially utilize $env:PATH to directly access the PATH environment variable. This variable is crucial in determining where the operating system looks for executable files.

When we execute $env:PATH, we receive an output listing directories such as C:\Windows\system32 and others, indicating where the system searches for executable files.

Output:

print environment variables in powershell - output 1

Printing All Environment Variables Using the Get-ChildItem Command

For a comprehensive overview of all environment variables, we employ the Get-ChildItem cmdlet. This command lists all the environment variables available in your PowerShell session.

Get-ChildItem Env:

In our utilization of the Get-ChildItem Env: command in Windows PowerShell, we effectively list all the environment variables present in the current session. This command is akin to taking an inventory of the settings and configurations that the operating system and applications use.

By executing Get-ChildItem Env:, we are presented with a comprehensive list, including each variable’s name and value.

Output:

print environment variables in powershell - output 2

Printing Environment Variables With Piping

The flexibility of PowerShell allows us to combine Get-ChildItem with other commands using piping. For instance, to export a list of all environment variables to a CSV file, we can execute:

Get-ChildItem Env: | select Name | Export-Csv -Path \env_variables.txt -NoTypeInformation

First, we employ Get-ChildItem Env: to retrieve all environment variables, just as we would list files in a directory. By piping (|) this output to select Name, we focus solely on the names of these variables, omitting their values for a cleaner, more concise overview.

Finally, we channel this refined list into Export-Csv to create a CSV file at C:\path\env_variables.txt, choosing to exclude type information with -NoTypeInformation.

Output:

print environment variables in powershell - output 3

Printing Specific Environment Variables

We can also output a specific environment variable using the native Get-ChildItem command. Try running the example command below to output the HOME file path.

Get-ChildItem Env:HOME

We use the command Get-ChildItem Env:HOME to specifically target and retrieve information about the HOME environment variable. This command is akin to zooming in on a particular aspect of our system’s environment, allowing us to understand where the HOME directory is set.

By executing this command, we receive detailed information about the HOME variable, which typically includes its name, value, and sometimes additional details depending on the system configuration.

Output:

print environment variables in powershell - output 4

Printing Environment Variables Using Alias

PowerShell provides several aliases for commonly used cmdlets to streamline the command execution process.

These can be particularly handy for users transitioning from other command-line environments or for simplifying command syntax.

Example Code:

Get-Alias -Definition Get-ChildItem

This command reveals all the aliases associated with Get-ChildItem, which is a versatile cmdlet used for listing items in a directory, including environment variables. By executing this, we gain insight into more intuitive or familiar command names, like dir, ls, and gci, that we can use interchangeably with Get-ChildItem.

Output:

print environment variables in powershell - output 5

For this reason, we can use the commands dir, gci, and ls in exchange for the native cmdlet Get-ChildItem.

Example Code:

dir env:
gci env: | select name
ls env:USERSPROFILE | Export-Csv -Path env_variables.txt -NoTypeInformation

We employ three distinct but related commands: dir env:, gci env: | select name, and ls env:ALLUSERSPROFILE | Export-Csv -Path C:\path\env_variables.txt -NoTypeInformation.

With dir env:, we are essentially listing all environment variables, leveraging dir as an alias for Get-ChildItem. This offers us a quick and comprehensive view of the environment settings.

Moving to gci env: | select name, we refine our approach by not only listing the environment variables using gci (another alias for Get-ChildItem) but also piping the output to select name to extract just the names of these variables, providing a cleaner and more focused output.

Lastly, ls env:ALLUSERSPROFILE | Export-Csv -Path C:\path\env_variables.txt -NoTypeInformation takes this a step further. Here, we use ls (yet another alias) to target a specific variable (ALLUSERSPROFILE), and then we export its details to a CSV file, choosing a specific path and excluding type information for simplicity.

Output:

print environment variables in powershell - output 6

Conclusion

As we conclude our journey through the manipulation and management of environment variables in Windows PowerShell, it is evident that these variables are more than mere settings; they are pivotal components that orchestrate the smooth functioning of our systems. Our exploration has equipped us with valuable insights and practical skills, from retrieving specific environment variables to employing aliases for streamlined command execution.

The ability to not only understand but also manipulate these variables is an indispensable skill in the realm of system administration and software development. We’ve seen firsthand how PowerShell’s versatility allows for detailed inquiry and customization of our computing environment.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell Environment Variables