How do I repoint Developer Powershell to 2022 devenv.exe - visual-studio

To reproduce a developer powershell bug, follow the steps below:
Uninstall Visual Studio 2019
Install Visual Studio 2022
Open Visual Studio 2022
Right-Click a project
Click "Open in Terminal" in a menu
Developer PowerShell opens and displays the error message:
vs : The term 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At C:\Users\jdoe\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:340 char:2
+ vs "C:\EDW\My Project\My Project.sl ...
+ ~~
+ CategoryInfo : ObjectNotFound: (C:\Program File...\IDE\devenv.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
**********************************************************************
** Visual Studio 2022 Developer PowerShell v17.4.4
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
How do I fix it?

There's probably (and hopefully) a better solution, but here's a pragmatic one:
Create directory C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7 and define a symlink named IDE in it that targets the analogous directory in Visual Studio 2022.
Note that elevation is required:
$oldDir = 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE'
# Adapt as needed
$newDir = 'C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\Common7\IDE'
# Create the parent dir. of the missing old dir.
$oldParentDir = New-Item -Type Directory -Force (Split-Path -Parent $oldDir) -ErrorAction Stop
# Create symlink for the last component that points to the desired dir.
New-Item -Type SymbolicLink $oldDir -Target $newDir -ErrorAction Stop

Related

VS 2019 Compilation error using Windows SDK and wdk on windows containers

I'm compiling a solution and have made the following change in the .vcxproj file <PlatformToolset>WindowsApplicationForDrivers10.0</PlatformToolset> from
<PlatformToolset>v142</PlatformToolset>
I have VS 2019 professional and the Windows 10 1903 wdk and windows 10 sdk 19041 installed and I keep getting the following error when I try to compile the solution file, Any pointers on what am I doing wrong here are greatly appreciated!
error MSB8020: The build tools for WindowsApplicationForDrivers10.0 (Platform Toolset = 'Windows ApplicationForDrivers10.0') cannot be found. To build using the WindowsApplicationForDrivers10.0 build tools, please install WindowsApplicationForDrivers10.0 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution".
The command to compile my solution file is as follows on the windows containers
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat" && msbuild myproject.sln /m /p:Platform=x64 /p:Configuration="Release"
I had to install the wdk.vsix (wdk extension for Visual studio) manually after installing the WDK into the VS installation location. Following are the powershell commands to do this:
Copy-Item ""${Env:ProgramFiles(x86)}\Windows Kits\10\Vsix\WDK.vsix"" 'C:\wdkvsix.zip'
Expand-Archive 'C:\wdkvsix.zip' -DestinationPath 'C:\WdkVsix'
Copy-Item 'C:\WdkVsix$VCTargets\Platforms*' -Destination 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Platforms' -Recurse -Force

run vcvarsall, but cl and other still unavailable

As titled, using Powershell after executing
PS C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build> .\vcvarsall.bat amd64
then programs like cl, nmake or msbuild should be available on the path, but they are not
PS C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build> cl
cl : The term 'cl' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
cl
~~
CategoryInfo : ObjectNotFound: (cl:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
Specs:
VS 2019 Community Edition with the "Desktop development with C++" workload:
MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.28)
Windows 10 SDK (10.0.18362.0)
Just-In-Time debugger
C++ profiling tools
C++ CMake tools for Windows
C++ ATL for latest v142 build tools (x86 & x64)
Test Adapter for Boost.Test
Test Adapter for Google Test
Live Share
C++ AddressSanitizer (Experimental)
Win10 Pro N x64, 20H2, 19042.746
What am I missing?
Powershell executes the batch file in a child cmd process. The VS environment is set in that cmd process, then gets discarded when the process ends and control returns to Powershell.
The straightforward solution is to reverse the sequence, and set the VS environment before starting the Powershell session so that PS inherits it. Assuming a usual VS installation with the correct VS160COMNTOOLS environment variable set, this can be done by running the following at either a command prompt, via Start / Run, or from PS itself.
cmd /c ""%VS160COMNTOOLS%\..\..\VC\Auxiliary\Build\vcVarsAll.bat" amd64 && start powershell"
See also In a CMD batch file, can I determine if it was run from powershell? and How can I use PowerShell with the Visual Studio Command Prompt? for more discussion and possible alternatives - both for returning environment variables from batch files to PS in general, and for the VS environment in particular.

Building CUDA in platformio-ide-terminal

I would like to be able to compile and build CUDA C source code provided here using Windows 10 Powershell. I have no issue doing this using x64 Native Tools Command Prompt for VS 2017.
However, I have tried several ways suggested online to get Powershell to work, but no success. The reason is I want to be able to build my cuda codes in Atom, the editor, using its package platformio-ide-terminal that loads a Powershell inside Atom. So, if I figure out how to setup Visual Studio 2017 Community in a Powershell, I edit my code in Atom and conveniently build them using its Powershell integration.
I tried to set the environment as follows but still nvcc cannot find the path to cl.exe.
Could someone kindly help me?
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\AFP\Downloads\cuda_by_example> cd "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\"
PS C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build> .\vcvarsall.bat amd64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.7.3
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
PS C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build> cd C:\Users\AFP\Downloads\cuda_by_example\
PS C:\Users\AFP\Downloads\cuda_by_example> nvcc .\chapter03\hello_world.cu
nvcc fatal : Cannot find compiler 'cl.exe' in PATH
PS C:\Users\AFP\Downloads\cuda_by_example>
You can add the path to cl.exe to the environment variables:
control panel > view advanced system settings > Environment variables > Path > New.
Then add C:\Program Files (x86)\Microsoft Visual Studio\<year >\Community\VC\Tools\MSVC\<toolset>\bin\Hostx64\x64.
Or use this command for compile: nvcc x.cu .\chapter03\hello_world.cu -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\<year >\Community\VC\Tools\MSVC\<toolset>\bin\Hostx64\x64"
Open a Windows Powershell and run the following commands at the prompt:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\AFP\Downloads\cuda_by_example> & 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat' amd64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.7.3
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
PS C:\Users\AFP\Downloads\cuda_by_example> $ENV:PATH="$ENV:PATH;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx64\x64"
PS C:\Users\AFP\Downloads\cuda_by_example> nvcc .\chapter03\hello_world.cu
hello_world.cu
Creating library a.lib and object a.exp
PS C:\Users\falah\Downloads\cuda_by_example> .\a.exe
Hello, World!
PS C:\Users\falah\Downloads\cuda_by_example>
Automation
To automate this, create a file named nvcc_setup_for_powershell.ps1 and place the following two commands in it.
& 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat' amd64
$ENV:PATH="$ENV:PATH;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\Hostx64\x64"
Write-Output "Configured PowerShell for NVCC Using Visual Studio 2019 Community x64"
$myshell = New-Object -com "Wscript.Shell"
$myshell.sendkeys("{ENTER}")
the last two commands is to simulate hitting enter button, adopted from here.
Open settings for platformio-ide-terminal under Core > Run Command place the path to the PowerShell script: & "C:\Path\To\Script\nvcc_setup_for_powershell.ps1".
You may need to open a PowerShell as administrator and run the following command
Set-ExecutionPolicy RemoteSigned
to allow you to execute nvcc_setup_for_powershell.ps1.

How to update Visual Studio 2017 using command line?

I'd like to automatically update my Visual Studio 2017 installation from a script (running this script at session login).
vs_installer shows a bunch of command line arguments (using /?). So I tried :
& "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" update --passive --norestart
However, nothing happens. I see the process in the task manager for a few seconds, but the product is not updated.
How to update my VS installation ?
Is is possible to handle the installer update ?
PS: if possible, I'd to have a passive update, not an invisible update. I'd prefer seeing the product being updated to avoid launching a new instance while the update is in progress.
I found partially the answer. I have to specify the install path of visual studio to let the installer knows what to update:
& "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" update --passive --norestart --installpath "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise" (assuming default path).
I still have to look for updates of the installer itself, but since I've the latest version, I've to wait for a new release.
[Edit 04/10/2019] The update process is similar with VS 2019 (at least from 16.0 to 16.0.1). I can update both version using :
Start-Process -Wait -FilePath "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" -ArgumentList "update --passive --norestart --installpath ""C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise"""
Start-Process -Wait -FilePath "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" -ArgumentList "update --passive --norestart --installpath ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise"""
I use Start-Process with -Wait to avoid returning control before the end of the update.

Visual Studio: missing Shortcut: "Visual Studio Command Prompt"

Because the VS 2015 Installer was crashing all the time, I had to use the silent installation. Now I don't have the "Visual Studio Command Prompt" Shortcut in Start menu. Can I find it somewhere else?
I had to create the shortcuts manually as well, but they all follow the same pattern so no biggie:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" x86
Don't forget to change the execution path to: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\"
Replace the last bit (x86) with the tool you want to use:
x86
amd64
arm
x86_arm
x86_amd64
amd64_x86
amd64_arm
Visual studio command prompt is nothing but the regular command prompt where few environment variables are set by default. This variables are set in the batch script : C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat . So basically to get a visual studio command prompt for a particular version, just open regular command prompt and run this batch script : C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat (Change the visual studio version based on your installed version). Voila you have got the visual studio command prompt. You can write a script to run the batch file and open cmd.exe and have a shortcut for the same.

Resources