How can I set the environment variables to run a c++ programme from the command window when winsdk.bat fails to set WindowsSdkDir variables correctly?

Jon 0 Reputation points
2025-10-31T10:30:27.02+00:00

When I run the VsDevCmd.bat file to configure a command window to allow me to compile C++ programs the following variables are NOT set:

  1. WindowsSdkDir
  2. WindowsSDKVersion
  3. WindowsSDKBinPath
  4. WindowsSDKVerBinPath

This is because VsDevCmd.bat calls winsdk.bat which attempts to set these variables by querying the registry. My permissions do not allow me to do this. I guess this is due to how the Group Policy has been set and which won't be changing anytime soon because of security concerns relating to accessing the registry.

In VsDevCmd the call to winsdk is made by "core_winsdk" at around L96.

In winsdk the registry query is made by "GetWin10SdkDirHelper" at around L87 and the query is as follows:

for /F "tokens=1,2*" %%i in ('reg query "%1\Microsoft\Microsoft SDKs\Windows\v10.0" /v "InstallationFolder"') DO (

if "%%i"=="InstallationFolder" (

    SET WindowsSdkDir=%%~k

)

)

The version of VS / Build Tools I am using is:

Visual Studio 2022 Developer Command Prompt v17.14.18

Could someone please suggest an alternative means of setting the 4 WindowsSDK variables listed above?

Secondly, why on earth would the developer of these batch scripts choose this method? Even as an administrator I cannot setup the command window, this must affect virtually everyone trying to use Build Tools for C++.

Any help much appreciated.

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

1 answer

Sort by: Most helpful
  1. Adiba Khan 1,210 Reputation points Microsoft External Staff
    2025-11-10T07:35:15.4433333+00:00

    Thank you for the detailed follow up and for confirming your findings. Based on your explanation, the issue Indeed appears to be caused by group policy restrictions that prevent the winsdk.bat file from executing the registry query (reg query ) command.

    Since you do not have permission to modify registry access or run with elevated privileges, the recommended approach is to bypass registry dependency by using static or preconfigured SDK paths without requiring reg query.

    Recommended workarounds

    **1.      **manually defines SDK paths in a custom script

    central registry cannot be queried in your environment; you can define the required SDK variables manually in a separate batch file and run it before building your C++ project.

    For example, create a file named SetWindowsSDKEnv.bat with the following contents:

    @echo off
    Rem ---- Manually configure Windows SDK paths ----
    Set “WIndowsSdkDir-C:\Program Files (x86)\Windows Kits\10”
    Set “WindowsSdkVersions=10.0.22621.0”
    Set “WindowsSdkBinPath=%WindowsSdkDir%Bin\%WindowsSdkVersion%”
    Set “WindowsSdkLibPath=%WindowsSdkDir%Lib\%WindowsSdkVersion%”
    Set “WindowsSdkIncludePath=%WindowsSdkDir%Include\%WindowsSdkVersion%”
    Echo Windows SDK environment configured successfully.
    
    

    Update the WindowsSdkVersion to match the version install on your system. You can find this by checking the folder name inside C:\Program Files (x86)\ WindowsKits\10\Include.

    Then run:

    Call SetWIndowSDKEnv.bit
    
    

    Before running your build commands.

    This ensured your SDK variables are correctly initialized without needing registry access.

    **2.      **User pre initialized build environment

    If we are organization uses build agents or CI/CD systems. You can predefine these environment variables at the system level:

    • open system properties -> advanced-> environment variables
    • under system variables, click new and add same 4 variables: o  WindowsSdkDir o  WindowsSdkVersion o  WindowsSdkBinPath o  WindowsSdkLibPath
    • Save and restart your shell or build environment.

    This method ensures all users/machines have consistent SDK paths without modifying registry access.

    **3.      **Avoid dependency on winsdk.bat

    you can also invoke the compiler directly using the Visual Studio build tools vcvarsall.bat (which does not rely on reg query for SDK detection):

    Example:

    Call “C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VS\Auxiliary\Build\vcvarsall.bat” x64
    
    

    Then manually ensure SDK paths are defined as above.

    https://learn.microsoft.com/en-us/cpp/build/building-on-command-line

    Why this happens

    The group policy setting prevent access to registry editing tools or restricted HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows registry access prevents scripts from reading SDK locations. Winsdk.bat internally relies on these registry keys to set the correct variables.

    Since you confirmed the policy cannot be changed, the static variable configuration is the correct long-term workaround.

    Please let us know if you require any further assistance we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer".

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.