Remove unwanted (empty) xmlns attribute added by appendChild

As answered by Michael Kay, the best way to remove this unwanted namespace is creating the new child element in the same namespace as its parent:

function setupProject($projectFile) {
  [xml]$root = Get-Content $projectFile;

  $project = $root.Project;

  # UPDATE THIS LINE $beforeBuild = $root.CreateElement("Target", "");
  $beforeBuild = $root.CreateElement("Target", $project.NamespaceURI);
  $beforeBuild.SetAttribute("name", "BeforeBuild");
  $beforeBuild.RemoveAttribute("xmlns");
  $project.AppendChild($beforeBuild);

  $root.Save($projectFile);
}

The xmlns="" namespace (un)declaration has been added because your parent element is in a namespace and your child element is not.

If you don't want this namespace declaration added, the implication is that you want the child element to be in the same namespace as its parent, and the answer is to put it in this namespace at the time you create the element. That is, change the call CreateElement("Target", "") to specify the correct namespace.