With previous versions of Visual Studio, I used Kevin Pilch-Bisson\'s script to format all C# files in my solution.
VS2012 dropped macro support, so that doesn\'t work a
Here's another variant of the previous two answers that users may find helpful... definitely could be further improved and simplified. This not only formats .cs files but also .json, .cshtml, .js, and .css.
function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".cs" ) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }
$dte.Solution.Projects | % { f($_.ProjectItems) }
function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".json" ) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }
$dte.Solution.Projects | % { f($_.ProjectItems) }
function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".cshtml" ) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }
$dte.Solution.Projects | % { f($_.ProjectItems) }
function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".js" ) -and (-not $_.Properties.Item("FullPath").Value.Contains("common")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Content")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Scripts")) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }
$dte.Solution.Projects | % { f($_.ProjectItems) }
function f($projectItems) { $projectItems | ? { $_.Name -ne $null -and $_.Name.EndsWith( ".css" ) -and (-not $_.Properties.Item("FullPath").Value.Contains("common")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Content")) -and (-not $_.Properties.Item("FullPath").Value.Contains("Scripts")) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }
$dte.Solution.Projects | % { f($_.ProjectItems) }