powershellcommand-linetfstfvc

Execute code using 'Developer Command Prompt for VS 2019' via PowerShell script


There are certain 'tf' commands that only work within 'Developer Command Prompt for VS 2019' (DCP) and not in cmd or PowerShell.

The code I need to run via the DCP will be written dynamically by a Powershell script.

For example, the code could be getting the latest version of files within a folder in DevOps source control. The commands for this would be as follows:

cd "C:\someroot\foldername"
tf get 

When I execute the PowerShell script, it would determine what 'foldername' is required and then create these lines of code. This is a simplified example.

What script do I require to run the code automatically from within the PowerShell using the DCP?

The DCP window is opened from the following: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat

I would welcome any suggestions of how to run either individual lines of code or a batch file direct from a PowerShell script using the DCP (Developer Command Prompt for VS 2019).

EDIT:

When I manually open the Developer Command Prompt via the provided shortcut, the window opens with the current directory set to: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional. I can successfully change this using the command cd "C:\root\folder name". The command tf get then also works, getting the latest files for files in that folder in source control. I've tried using Start-Process with these commands separated by a semi-colon in -ArgumentList. However, this doesn't work and returns "Invalid command line argument: 'cd'".

Attempts:

Start-Process -FilePath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat" -ArgumentList "cd ""C:\root\folder name"" ; tf get" 

Start-Process -FilePath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat" -WorkingDirectory "C:\root\folder name" -ArgumentList "tf get"

I added -RedirectStandardOutput "c:\standardoutput.txt" to the above commands to record the output.

I also attempted accessing the Developer Command Prompt via the shortcut but couldn't work out how to add code to run commands once the prompt was opened...

Invoke-Item "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\Developer Command Prompt for VS 2019.lnk"

Solution

  • The Start-Process cmdlet will start an executable. The required -FilePath parameter is the executable to be run (or a document file with a file extension that is mapped to an executable). The -ArgumentList parameter is arguments to be passed to the executable.

    On your system the 'Developer Command Prompt for VS 2019' shortcut is defined as:

    %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat"
    

    The path for VsDevCmd.bat will vary. For example, I have VS2022 Community on a machine and the path is "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat".

    On Windows, the vswhere tool or the VSSetup PowerShell module can be used to locate Visual Studio.

    %comspec% is an environment variable. On current versions of Windows, it is generally set to the path and file name of cmd.exe.

    The Dev Prompt shortcut is running the cmd.exe executable with an argument list of /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat". The /k parameter keeps the command shell running after executing a command. In this case the VsDevCmd.bat file is executed.

    Mapping the shortcut to Start-Process, the -FilePath would be "$env:comspec" and the -ArgumentList would be /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat".

    Changing the /k to /c causes cmd to exit after executing the command.

    In the Windows batch language, two or more commands can be provided in one line by separating the commands with an &. The following will run tf get after running the VS batch file:

    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat" & tf get
    

    Start-Process has a -WorkingDirectory parameter which sets the current directory for the new process.

    Bringing this all together looks like the following.

    Start-Process -FilePath "$env:comspec" -ArgumentList "/c `"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat`" & tf get" -WorkingDirectory "C:\root\folder name"
    

    (Note that the -ArgumentList string is enclosed within double quotes but double quotes are also needed for the 'Program Files (x86)' path. Within the string, double quotes are escaped with a backtick.)

    Another approach would be to locate Visual Studio. The tf.exe tool will be in a standard location within the Visual Studio installation. If the path and name of tf.exe is determined, the tf command could be run directly without using the command shell.

    Given variables $Tf and $Workspace for the path and name of tf and for the workspace directory, respectively:

    Start-Process -FilePath "$Tf" -ArgumentList "get" -WorkingDirectory "$Workspace"
    

    An example value for $Tf would be "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe".

    An example value for $Workspace would be "C:\root\folder name".