How to use the command 'vswhere' (with examples)

How to use the command 'vswhere' (with examples)

This article will illustrate several use cases of the command ‘vswhere’ for locating Visual Studio 2017 and newer installations.

Command Description

The ‘vswhere’ command is a versatile tool provided by Microsoft for locating Visual Studio installations on a system. It allows users to find the paths of various files and folders associated with Visual Studio, such as ‘vcvarsall.bat’, ‘MSBuild.exe’, and various compilers like ‘cl.exe’ and ‘clang-cl’. This command is especially helpful for developers who rely on Visual Studio for their development workflows and need to set up their environment variables or locate specific tools bundled with the Visual Studio installation.

Use case 1: Find the path of vcvarsall.bat to set environment variables

Code:

vswhere -products * -latest -prerelease -find **\VC\Auxiliary\Build\vcvarsall.bat

Motivation:

When working with certain programming languages or tools, it is often necessary to set up environment variables correctly. One of the most crucial environment variables to be set is the path to ‘vcvarsall.bat’, which assists in configuring the build environment. By using the ‘vswhere’ command, developers can easily locate the ‘vcvarsall.bat’ file, ensuring that their build environment is properly configured.

Explanation:

  • -products *: This argument specifies that the command should search for all available Visual Studio installations.
  • -latest: This argument ensures that the command will only consider the latest version of Visual Studio found on the system.
  • -prerelease: This argument allows the command to consider pre-release versions of Visual Studio.
  • -find **\VC\Auxiliary\Build\vcvarsall.bat: This argument specifies the file to be found using wildcards. In this case, the command will search for ‘vcvarsall.bat’ file located within the ‘VC\Auxiliary\Build’ folder.

Example output:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat

Use case 2: Find the directory of the x64 MSVC compiler (cl.exe, etc)

Code:

vswhere -products * -latest -prerelease -find **\Hostx64\x64\*

Motivation:

Developers often need to locate the directory where the MSVC (Microsoft Visual C++) compiler is installed. This is crucial when working on projects that require custom compilation steps outside of an integrated development environment. By using the ‘vswhere’ command, developers can easily find the directory containing the MSVC compiler binaries, such as ‘cl.exe’.

Explanation:

  • -products *: This argument specifies that the command should search for all available Visual Studio installations.
  • -latest: This argument ensures that the command will only consider the latest version of Visual Studio found on the system.
  • -prerelease: This argument allows the command to consider pre-release versions of Visual Studio.
  • -find **\Hostx64\x64\*: This argument specifies the directory to be found using wildcards. In this case, the command will search for the ‘Hostx64\x64’ directory and return any subdirectories or files within it.

Example output:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.30.30704\bin\Hostx64\x64

Use case 3: Find the directory of Clang bundled with Visual Studio bundled (clang-cl, clang-tidy, etc)

Code:

vswhere -products * -latest -prerelease -find **\Llvm\bin\*

Motivation:

Visual Studio provides support for Clang, a popular LLVM-based C/C++ compiler. When working with Clang-based tooling like ‘clang-cl’ or ‘clang-tidy’, it is crucial to locate the directory where Clang is installed. By utilizing the ‘vswhere’ command, developers can effortlessly find the directory containing Clang executables bundled with Visual Studio.

Explanation:

  • -products *: This argument specifies that the command should search for all available Visual Studio installations.
  • -latest: This argument ensures that the command will only consider the latest version of Visual Studio found on the system.
  • -prerelease: This argument allows the command to consider pre-release versions of Visual Studio.
  • -find **\Llvm\bin\*: This argument specifies the directory to be found using wildcards. In this case, the command will search for the ‘Llvm\bin’ directory and return any subdirectories or files within it.

Example output:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\x64\bin

Use case 4: Find the path of MSBuild.exe

Code:

vswhere -products * -latest -prerelease -find MSBuild\**\Bin\MSBuild.exe

Motivation:

MSBuild is a build tool provided by Microsoft, and it is commonly used to build, deploy, and test .NET applications. However, the path to ‘MSBuild.exe’ may vary depending on the installed Visual Studio version. By using the ‘vswhere’ command, developers can easily locate the correct version of ‘MSBuild.exe’ regardless of the Visual Studio installation path.

Explanation:

  • -products *: This argument specifies that the command should search for all available Visual Studio installations.
  • -latest: This argument ensures that the command will only consider the latest version of Visual Studio found on the system.
  • -prerelease: This argument allows the command to consider pre-release versions of Visual Studio.
  • -find MSBuild\**\Bin\MSBuild.exe: This argument specifies the file to be found using wildcards. In this case, the command will search for ‘MSBuild.exe’ file located within the ‘MSBuild’ folder at any depth.

Example output:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe

Conclusion

The ‘vswhere’ command is a powerful tool that enables developers to easily locate various files and directories associated with Visual Studio installations. With the ability to search for specific files or folders using wildcards, developers can efficiently set up their development environment or find the necessary tools for their workflows. Whether it’s locating ‘vcvarsall.bat’, compiler binaries, Clang executables, or ‘MSBuild.exe’, the ‘vswhere’ command simplifies the process of finding these crucial components of Visual Studio installations.

Related Posts

How to use the command 'daemonize' (with examples)

How to use the command 'daemonize' (with examples)

The “daemonize” command allows you to run another command as a Unix daemon.

Read More
How to use the command logwatch (with examples)

How to use the command logwatch (with examples)

Logwatch is a command-line tool that helps summarize and analyze various logs for common services.

Read More
How to use the command 'ddev' (with examples)

How to use the command 'ddev' (with examples)

The ‘ddev’ command is a container-based local development tool specifically designed for PHP environments.

Read More