title: TFS 2015 API remove agent from pool with PowerShell
body: <p>I'm working on removing an agent from a pool temporarily, install new software on the buildserver the agent is on, test that it works, and then add the agent to the pool again.</p>
  
  <p>I would like to do that programmatically, either with PowerShell or if that isn't a possibility, then do it with C#.</p>
  
  <p>The problem is that I can't find any documentation that can assist me on doing this, either through the TFS REST API or through the tools that come with Visual Studio.</p>
  
  <p>So I'm specifically asking:</p>
  
  <p>How do I remove a named agent from a build pool and
  how do I add a named agent back into the build pool?</p>
  
  <p>I basically want the same functionality of going onto the web administration of TFS and unchecking/checking an agent in the pool.</p>
  
  <p>When I try to enable/disable an agent with the information provided by starain-msft, I get the following error:</p>
  
  <pre><code>Invoke-RestMethod :
  404 - File or directory not found.
  Server Error
  </code></pre>
  
  <p>Later I removed much of the error, as I found out that issue lay in my company's proxy.
  Read here: <em><a href="https://www.visualstudio.com/en-us/docs/integrate/get-started/rest/basics#http-method-override" rel="nofollow noreferrer">Azure DevOps Services REST API Reference</a></em></p>
  
  <p>But I got it to work with the help of starain-msft.</p>
  
  <p>The final solution looks like this:</p>
  
  <pre><code>Function TFSwebRequest {
      param
      (
          [ValidateNotNullOrEmpty()]
          [Parameter(Mandatory = $true)]
          [string] $Uri,
  
          [ValidateNotNullOrEmpty()]
          [Parameter(Mandatory = $true)]
          [string] $Method,
  
          [ValidateNotNullOrEmpty()]
          [string] $ContentType,
  
          [ValidateNotNullOrEmpty()]
          [string] $ContentBody,
  
          [ValidateNotNullOrEmpty()]
          [System.Net.WebHeaderCollection] $Headers
      )
  
      # Creating Webrequest from 'Uri'
      $webRequest = [System.Net.HttpWebRequest]::CreateHttp($Uri)
  
      $webRequest.UseDefaultCredentials = $true
      $webRequest.Method = $Method
      if ($Headers.Count -ne 0) {
          $webRequest.Headers = $Headers
      }
      if (![string]::IsNullOrEmpty($ContentType)) {
          $webRequest.ContentType = $ContentType
      }
      if (![string]::IsNullOrEmpty($ContentBody)) {
          $Body = [byte[]][char[]]$ContentBody
          $Stream = $webRequest.GetRequestStream();
          $Stream.Write($Body, 0, $Body.Length);
      }
  
      # Get webresponse to a variable
      try {
          [System.Net.WebResponse]$webResponse = $webRequest.GetResponse()
      }
      catch {
          $ErrorMessage = $_.Exception.Message
          Write-Host "TFSwebRequest Failed = " $ErrorMessage -ForegroundColor Red
      }
  
      # Stream webresponse to a string
      $webResponseStream = $webResponse.GetResponseStream()
      $streamReader = New-Object System.IO.StreamReader $webResponseStream
      $result = $streamReader.ReadToEnd() | ConvertFrom-Json
  
      return ,$result
  }
  
  $agentUri = "http://teamfoundation:8080/tfs/Main/_apis/distributedtask/pools/$($poolID)/agents/$($agentID)?api-version=2.3-preview.1"
  $contentBody = @"
  {
      "maxParallelism": 1,
      "id": INSERTID,
      "enabled": true #Or false
  }
  "@
  
  $headers = New-Object System.Net.WebHeaderCollection
  $headers.Add("X-HTTP-Method-Override", "PATCH")
  
  TFSwebRequest -Uri $agentUri -Method "POST" -Headers $headers -ContentType "application/json" -ContentBody $contentBody
  </code></pre>