title: Replace DFS folder target
body: <p>I am looking for a PowerShell script that is capable to change the file server name in folder targets of all DFS namespaces</p>
  
  <p>e.g. folder target '\\server1\Marketing\Branding' should become '\\server2\Marketing\Branding'</p>
  
  <p>This is what I've come up so, far but the replace does not what I want it to do. Also I believe the code could probably structured much simpler:</p>
  
  <pre><code>$Namespaces = Get-DfsnRoot
  
  $Namespaces | ForEach-Object { 
  
  $NSpath = $_.path
  $NSfullpath = $NSpath + '\*'
  $DFSTree = Get-DfsnFolder $NSfullpath
  
  $DFSTree | ForEach-Object {
      $DFSTarget = Get-DfsnFolderTarget $_.Path       
      $DFSTarget -replace "server1", "server2"
      Set-DfsnFolderTarget -Path $DFSTarget.Path -TargetPath $DFSTarget.TargetPath -WhatIf
      $DFSTarget -replace "server2", "server1"
      Remove-DfsnFolderTarget -Path $DFSTarget.Path -TargetPath $DFSTarget.TargetPath -WhatIf
      }
  }
  </code></pre>
  
  <p>Any help is much appreciated!</p>