Skip to content

Instantly share code, notes, and snippets.

@am11
Last active November 29, 2016 09:26
Show Gist options
  • Save am11/292b72071f265ab6da82c89c295ca9d5 to your computer and use it in GitHub Desktop.
Save am11/292b72071f265ab6da82c89c295ca9d5 to your computer and use it in GitHub Desktop.
How to mass-edit *proj files in VS using PowerShell

How to mass-edit *proj files in VS

  • In powershell, we first rename all csproj files to csprojxxx; so VS does not try to open them as projects instead treat them as text files:

    ls -r *.csproj | rename-item -force -newname { [io.path]::ChangeExtension($_.name, "csprojxxx") }
  • In File Explorer, navigate to project directory and search for *.csprojxxx, then drag all the items onto a new VS instace.

  • In VS, use regex to capture a whole line by a keyword, for example (^.*Microsoft\.Common\.props.*\n) will select the entire line:

    (note two white spaces before the text)

      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

    then the replace text:

    (note two white spaces before the text)

      <Import Project="$(SolutionDir)\My.Common.props" />\n$1

    this will form:

    (note two white spaces before each line)

      <Import Project="$(SolutionDir)\My.Common.props" />
      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  • Finally rename all csprojxxx back to csproj

    ls -r *.csprojxxx | rename-item -force -newname { [io.path]::ChangeExtension($_.name, "csproj") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment