Peter Girnus

View Original

Windows bashrc File: PowerShell Equivalent

While the Windows Operating system does not have a bashrc file, an equivalent Windows $PROFILE variable contains paths to the PowerShell profiles available in the current session. We can use this $PROFILE variable to build an equivalent bashrc file and refresh this file through the Windows terminal.

Install Visual Studio Code

To edit our Windows PowerShell user profile, we will use Visual Studio Code. We can easily accomplish this installation through the Windows Package Manager (WinGet).

Search For Visual Studio Code

To search for the available Visual Studio Code package in the Windows Package Manager Repository, we can perform the following search from the Windows Terminal.

See this content in the original post

This command will list all matches for vscode in the Windows Package Manager open-source repository.

Install Visual Studio Code

We can easily install Visual Studio Code on Windows 10 and Windows 11 by executing the following command from the Windows Terminal.

See this content in the original post

This command will go through the entire Visual Studio code installation process directly from the Windows Terminal using the Windows Package Manager (WinGet).

Verify Visual Studio Code Is Installed

We can easily verify if Visual Studio was installed by performing the following command from the Windows Terminal. This will give us the source of the Microsoft Visual Studio code.cmd binary.

See this content in the original post

Visual Studio code provides the code.cmd command which allows users to launch Visual Studio Code through the Terminal. This command verifies that this command has been set.

Open Visual Studio Code From Windows Terminal

We can perform the following command to open Visual Studio Code from the command-line terminal.

See this content in the original post

or

See this content in the original post

These commands can be used to launch Visual Studio Code from the Windows Terminal.

Windows .bashrc Equivalent 

Windows does not have a .bashrc file, as this is a configuration file for Bash shells on Linux. Windows does, however, provide a series of PowerShell profiles. These include the CurrentUserCurrentHost profile stored in the $PROFILE variable.

See All Windows Profile Paths

We can check which Windows $PROFILE paths are available to us by running the following PowerShell command through the Windows terminal.

See this content in the original post

This command will output the $PROFILE value paths in the Windows PowerShell console such as:

  • Current User, Current Host - $PROFILE

  • Current User, Current Host - $PROFILE.CurrentUserCurrentHost

  • Current User, All Hosts - $PROFILE.CurrentUserAllHosts

  • All Users, Current Host - $PROFILE.AllUsersCurrentHost

  • All Users, All Hosts - $PROFILE.AllUsersAllHosts

Edit Windows .bashrc Equivalent

To edit Windows CurrentUserCurrentHost $PROFILE, which is the Windows PowerShell functional equivalent to the Linux .bashrc file, we can run the following command.

See this content in the original post

Once our PowerShell profile is open let's add a couple of useful functions to emulate Linux further.

Windows Add a Reload Profile Function

In our opened .bashrc equivalent add the following function.

See this content in the original post

This will allow us to run the function reload-profile from our Windows Terminal to refresh our Windows $PROFILE. It is helpful if we want to edit this file and quickly refresh the Windows Terminal with new changes to our $PROFILE.

Windows Add a grep Function

We can add a function to our Windows $PROFILE that allows us to perform a RegEx search on files, similar to Linux grep functionality. In our opened .bashrc equivalent, add the following function.

See this content in the original post

This function is useful for looking for useful items in files, such as IP Addresses. You can use this function in the following example.

See this content in the original post

In this example, we can use our grep function to fetch all URL’s in a given file.

Refresh Windows Profile

Once we are done editing our Windows $PROFILE which is the bashrc functional equivalent, we can refresh our Terminal with the changes we've added to our $PROFILE. Run the following command.

See this content in the original post

This is the functional equivalent of running source .bashrc on Linux. Once we have refreshed our Windows Terminal to use our changes to our $PROFILE we can call the functions we added such as.

See this content in the original post

This function will reload our $PROFILE file without having to call . $PROFILE. We can even tab to this command!

PowerShell .bashrc: Wrapping Up

In conclusion, this guide gives a step-by-step process for Windows users to create a customized environment within the Windows Terminal, mirroring the functionality of a .bashrc file on Linux systems. By leveraging PowerShell $PROFILES feature and Visual Studio Code, users can enhance their Windows Terminal experience with custom functions and commands.

This approach allows users to install and use Visual Studio Code efficiently and provides a way to add custom functions like reload-profile and grep to their PowerShell profiles. These functions enhance the Windows Terminal, making it more versatile and Linux-like. The ability to edit the PowerShell profile using Visual Studio Code (or any test editor) provides an intuitive and user-friendly experience, allowing for easy customization of the terminal environment.

Following the outlined steps, users can effectively manage their Windows PowerShell profiles, add valuable functions, and refresh their terminal environment to reflect the changes made. This guide allows Windows users to create a tailored and efficient command-line experience, bridging the gap between Windows and Linux environments.

See this social icon list in the original post