2

I want to create a new Solution Folder dynamically in Visual Studio using PowerShell script.

I know how to create a solution folder for an existing solution in Visual Studio manually but is there a way we can automate it using PowerShell?

1 Answer 1

2

To add a Solution Folder, you should add a Project entry. The Guid for the Project Type for a solution folder should be : "{2150E333-8FDC-42A3-9474-1A3956D46DE8}" and for the project Guid, you should use a new Guid.

Here is a function which adds a folder to a solution file:

Function AddFolderToSolution($folderName, $solutionFile)
{
   $solutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}"
   $content = [System.IO.File]::ReadLines($solutionFile)
   $lines = New-Object System.Collections.Generic.List[string]
   $lines.AddRange($content)
   $index = $lines.IndexOf("Global")
   $guid = [System.Guid]::NewGuid().ToString().ToUpper()
   $txt = "Project(`"$solutionFolderGuid`") = `"$folderName`", `"$folderName`", `"$guid`""
   $lines.Insert($index, $txt)
   $lines.Insert($index+1, "EndProject")
   [System.IO.File]::WriteAllLines($solutionFile, $lines)
}

And here is the usage:

AddFolderToSolution "NewFolder10" "D:\Solution1.sln"
Sign up to request clarification or add additional context in comments.

11 Comments

Where do I get the Guid?
The function contains all the logic. Just call it the same way that I called. The Guid for solution folder project type is "{2150E333-8FDC-42A3-9474-1A3956D46DE8}". The guid for the folder itself is a new Guid [System.Guid]::NewGuid().ToString().ToUpper(). The answer contains all required information you don't need to discover anything. I tried it with VS2017 and it worked properly.
I am getting this error: Exception calling "Insert" with "2" argument(s): "Index must be within the bounds of the List. Parameter name: index" At line:12 char:4 + $lines.Insert($index, $txt) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentOutOfRangeException
I'm running it against a VS2017 solution without any problem. Do some debug and make sure the solution file which you have is valid and for exampel contains "Global" entry.
@ANIL The solution folder is not a real physical folder. You don't need to create a folder. The solution folder is a logical folder meaningful just for categorizing some files, which may be located any where.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.