PowerShell script to enumerate deleted items in a TFS/Visual Studio Online project
I wanted to share the following PowerShell script (zipped script also available at the end of the post) that I often use to enumerate deleted files and folders in a TFS project hosted in my Microsoft Visual Studio Online TFS instance. Notice that the script uses the tf.exe command-line utility that gets installed with Microsoft Visual Studio. Specifically, my script expects Visual Studio 2015 to be installed in the default "C:\Program Files (x86)\Microsoft Visual Studio 14.0" folder. The TFS PowerShell cmdlets part of the Microsoft Visual Studio Team Foundation Server 2015 Power Tools may also be used to write a similar script.
$tfsUrl = Read-Host -Prompt "URL of your Visual Studio Online TFS (e.g. https://MyTfs.visualstudio.com)"
$tfsProject = Read-Host -Prompt "TFS Project Name (e.g. MyTfsProject)"
if($tfsUrl -eq "")
{
write-output "Provided TFS url is empty. Exiting script."
return
}
if($tfsProject -eq "")
{
write-output "Provided project name is empty. Exiting script."
return
}
elseif($tfsProject.StartsWith('$'))
{
write-output "Project name must not start with a dollar sign. Exiting script."
return
}
$deletedItemsCommand = '& "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\tf.exe" dir ' + '$\' + $tfsProject + ' /deleted /recursive /collection:' + $tfsUrl
#write-output $deletedItemsCommand
$deletedItems = Invoke-Expression $deletedItemsCommand
#write-output $deletedItems
$nl = [Environment]::NewLine
# DELETED FILES
$deletedFiles = @()
foreach($item in $deletedItems)
{
if ($item -like "$/*:")
{
$parentFolder = $item.Substring(0, $item.Length-1)
}
elseif ($item -like "*;X*")
{
$pos = $item.IndexOf(";X")
$itemName = $item.subString(0,$pos)
# Folders
if($itemName.StartsWith('$'))
{
continue
}
$itemPath = ($parentFolder + "/" + $itemName)
$deletedFiles += $itemPath
}
}
if($deletedFiles.Count -eq 0)
{
write-output "No deleted files found"
}
else
{
write-output ($nl + "**** FILES ****" + $nl)
foreach($itemPath in $deletedFiles)
{
write-output ($itemPath)
}
}
# DELETED FOLDERS
$deletedFolders = @()
foreach($item in $deletedItems)
{
if ($item -like "$/*:")
{
$parentFolder = $item.Substring(0, $item.Length-1)
}
elseif ($item -like "*;X*")
{
$pos = $item.IndexOf(";X")
$itemName = $item.subString(0,$pos)
# Folders
if($itemName.StartsWith('$'))
{
$itemName = $itemName.subString(1)
}
else
{
continue
}
$itemPath = ($parentFolder + "/" + $itemName)
$deletedFolders += $itemPath
}
}
if($deletedFolders.Count -eq 0)
{
write-output "No deleted folders found"
}
else
{
write-output ($nl + "**** FOLDERS ****" + $nl)
foreach($itemPath in $deletedFolders)
{
write-output ($itemPath)
}
}
You may want to redirect the output of this script to a file by executing it with the following command from a PowerShell command line:
EnumerateVsoProjectDeletedItems.ps1 > C:\Users\MyUser\Documents\DeletedItems.txt
The script will prompt you for the URL to your TFS server or Visual Studio Online TFS instance and will also prompt for the name of your TFS project.
DOWNLOAD: EnumerateVsoProjectDeletedItems
Comments
- Anonymous
September 02, 2017
Enjoyed reading through this, very good stuff, appreciate it.