Execute "dotnet new" command from PowerShell RRS feed

  • Question

  • I'm trying to create a .Net Core project using PowerShell and the "dotnet new" command. However, running this command from within PowerShell causes some problems, as PowerShell executes the command and moves on to the next and doesn't wait for it to finish.  I have looked through the various options to get PowerShell to wait, but none are actually working. I have tried various options like start-process -wait and other things, but nothing works. So I think I need some help with this one.

    The script below works if I debug through it, as I can control the timing. But if I run it strait through, it fails because it tries to add the project to the solution, before the projects have been created.

    Is there a good way to tell PowerShell to wait on the command to complete? Something like Start-Process -wait, that will work with this? My next step is to move it into a cmdlet and run the commands from C#. But that seems like overkill. The best option seems to be to just get PowerShell to work with this. I could also use batch, but I'm hoping I don't have to.

    Any suggestions would be much appreciated.

    Best regards,


    # Set Solution Name Variable
    New-Variable -Name "slnName" -Value "MyMvcApp1"
    New-Variable -Name "Path" -Value "H:\Solutions"
    # Create New Solution (automatically creates folder)
    dotnet new sln -o $Path\$slnName #| Out-Null
    # Set Current Directory 
    Set-Location -Path $Path\$slnName
    # Create Projects
    dotnet new mvc -o "$Path\$slnName\$slnName.Client" 
    dotnet new classlib -o "$Path\$slnName\$slnName.DataStore" 
    dotnet new mstest -o "$Path\$slnName\$slnName.DataStoreTest" 
    # Add Projects to Solution
    dotnet sln "$slnName.sln" add ".\$slnName.Client\$slnName.Client.csproj" ".\$slnName.DataStore\$slnName.DataStore.csproj" ".\$slnName.DataStoreTest\$slnName.DataStoreTest.csproj"
    dotnet sln "$Path\$slnName\$slnName.sln" add "$Path\$slnName\$slnName.Client\$slnName.Client.csproj" "$Path\$slnName\$slnName.DataStore\$slnName.DataStore.csproj" "$Path\$slnName\$slnName.DataStoreTest\$slnName.DataStoreTest.csproj"
    # Add References
    dotnet add ".\$slnName.Client\$slnName.Client.csproj" reference ".\$slnName.DataStore\$slnName.DataStore.csproj"
    dotnet add ".\$slnName.DataStoreTest\$slnName.DataStoreTest.csproj" reference ".\$slnName.DataStore\$slnName.DataStore.csproj"
    dotnet add "$Path\$slnName.Client\$slnName.Client.csproj" reference ".\$slnName.DataStore\$slnName.DataStore.csproj"
    dotnet add "$Path\$slnName.DataStoreTest\$slnName.DataStoreTest.csproj" reference ".\$slnName.DataStore\$slnName.DataStore.csproj"
    # Run Unit Tests
    #dotnet test "$Path\$slnName.DataStoreTest\$slnName.DataStoreTest.csproj"
    # Run Application
    #dotnet run -p .\MyWebApp.Client\MyWebApp.Client.csproj
    # Open Solution in Visual Studio
    start "$Path\$slnName\$slnName.sln"
    # Remove Variable
    Remove-Variable -Name "slnName"
    Remove-Variable -Name "Path"


    Jon Gregory Rothlander

    Thursday, June 6, 2019 7:23 PM