Search TFS the Easy Way – With PowerShell!

Posted on Updated on

Problem to Solve

Imagine yourself in this situation: you know there is a project in TFS that you need, but you can’t find it because the folder structure is confusing. There are hundreds of projects in there and you just can’t find it. If only there were some way to search every file in TFS, using wild cards etc., without checking-out anything!

Solution

Use PowerShell to search for a file name in the solution!

Step-by-Step Instructions

Suppose you know there is a project somewhere with a file name like ‘LostProject.cs’. It is taking you a long time to look for it.

Don't use a normal command prompt!  Use your Start Menu to get a special command prompt
Don’t use a normal command prompt! Use your Start Menu to get a special command prompt
  1. Open a Visual Studio Command Prompt (Start | Visual Studio 2010 | Visual Studio Tools | Visual Studio Command Prompt)
  2. Execute the following command:
  3. >tf dir /recursive $/*LostProj*.cs
  4. You will see a list of all the files in TFS that match the pattern above!

 

 

 

 

 

 

 

 

 

Now, Add Some Power

If you are good at DOS/PowerShell, you know that you can pipe your output into a text file. Then you can open the search results in your favorite text editor and do some fancier searches or processing. To do so, you would use the > character, which redirects your output to the file you specify, like this:

>tf dir /recursive $/ > C:\Users\Randy\Documents\Temp\AllTFSFiles.txt

The result of this command is that now you have a file containing a list of TFS files that match my original search pattern.

Two Sample Commands illustrating how to capture the search results into a file, after searching using a wildcard pattern
Two Sample Commands illustrating how to capture the search results into a file, after searching using a wildcard pattern

Taking it to The Next Level

It is not too hard to search the file contents as well, but that is a topic for another post. Suffice it to say that you can use PowerShell to get a copy of files, in bulk, from TFS, using wild cards, without checking-out said files. Then you can, for example, search inside those files for code samples, forgotten names, etc.

For example, I recently used this technique to get a list of all the connection strings, from every project in TFS. That made it possible to create an exhaustive list of every database we touched, for out entire department! The result was we were able to get organized and avoid making mistakes about which users have access to our databases.

One thought on “Search TFS the Easy Way – With PowerShell!

    […] I previously posted a blog article on using Power Shell to query TFS, in case you don’t need as much capability as provided by writing code. For this post, I will […]

Leave a comment