Download files from TFS server with PowerShell

If you want to download files from TFS with PowerShell, you will need to write a script that can access the TFS Server and access the folder on your drive.

This script uses a server path in the TFS server and download some files under that server path to the drop folder of your build. If you don’t use a build, you can change then environment variables. This script is created because of an original question on StackOverflow.

# The deploy directory for all the msi, zip etc.
$AutoDeployDir = "Your TFS Directory Server Path"
$deployDirectory = $($Env:TF_BUILD_DROPLOCATION + "\Deploy\" + $Env:TF_BUILD_BUILDNUMBER)

# Add TFS 2013 dlls so we can download some files
Add-Type -AssemblyName 'Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Add-Type -AssemblyName 'Microsoft.TeamFoundation.VersionControl.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$tfsCollectionUrl = 'http://YourServer:8080/tfs/YourCollection' 
$tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionUrl
$tfsVersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

# Register PowerShell commands
Add-PSSnapin Microsoft.TeamFoundation.PowerShell

# Get all directories and files in the AutoDeploy directory
$items = Get-TfsChildItem $AutoDeployDir -Recurse

# Download each item to a specific destination
foreach ($item in $items) {
    # Serverpath of the item
    Write-Host "TFS item to download:" $($item.ServerItem) -ForegroundColor Blue

    $destinationPath = $item.ServerItem.Replace($AutoDeployDir, $deployDirectory)
    Write-Host "Download to" $([IO.Path]::GetFullPath($destinationPath)) -ForegroundColor Blue

    if ($item.ItemType -eq "Folder") {
        New-Item $([IO.Path]::GetFullPath($destinationPath)) -ItemType Directory -Force
    }
    else {
        # Download the file (not folder) to destination directory
        $tfsVersionControl.DownloadFile($item.ServerItem, $([IO.Path]::GetFullPath($destinationPath)))
    }
}

 

5 thoughts to “Download files from TFS server with PowerShell”

  1. Need help ,

    I am using a network path for the variable $destinationPath and getting the below error.
    WARNING: Exception CategoryInfo : Exception calling “DownloadFile” with “2” argument(s): “The network path was not found”.Exception ..

    i used a localhost to D:drive ,
    Adding PowerShell Snap-in
    Finished Adding PowerShell Snap-in
    Download to \\LOCALHOST\d$\rops\Refresh
    Download to \\LOCALHOST\d$\rops\Refresh\xxxxxx.ps1

    Thank You

    1. Does it work if you don’t change the code? If so, you know that the path is the real problem. Don’t forget to set the security of the path. What is localhost? Localhost must be your pc. The TFS server needs to know what your pc is and the folder must be shared with it.

  2. i am using windows 2016 and i got the error “Add-PSSnapin Microsoft.TeamFoundation.PowerShell
    Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.”

    1. Same thing happened with me too. I came to know from some research that by re-installing Power Tools 2015 (tftp.exe) and explicitly selecting PowerShell option from the list may help resolve the problem but that option doesn’t even come up during the ‘Modify’ installation option.

      Please share any update if you come across with any solution around it.

      Thanks again!

Leave a Reply