Skip to content

Instantly share code, notes, and snippets.

@anotherlab
Created November 16, 2025 20:45
Show Gist options
  • Select an option

  • Save anotherlab/57de44266e0b16f1987dd82d4bb5bcc5 to your computer and use it in GitHub Desktop.

Select an option

Save anotherlab/57de44266e0b16f1987dd82d4bb5bcc5 to your computer and use it in GitHub Desktop.
param(
[Parameter(Mandatory=$true)]
[string]$NewProfileValue,
[Parameter(Mandatory=$false)]
[string]$csprojUserFile
)
# Locate the .csproj.user file if not specified
if (-not $CsprojUserFile) {
$foundFile = Get-ChildItem -Path . -Filter *.csproj.user | Select-Object -First 1
if (-not $foundFile) {
Write-Error "❌ No .csproj.user file found in the current directory."
exit 1
}
$CsprojUserFile = $foundFile.FullName
}
# Resolve to an absolute, full path
$fullPath = (Resolve-Path -Path $csprojUserFile).Path
# Load XML
$xmlContent = Get-Content -Path $fullPath -Raw
[xml]$xml = $xmlContent
# Detect the MSBuild namespace if present
$nsUri = $xml.Project.NamespaceURI
$hasNamespace = -not [string]::IsNullOrWhiteSpace($nsUri)
if ($hasNamespace) {
$nsMgr = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$nsMgr.AddNamespace("msb", $nsUri)
}
# Find the ActiveDebugProfile element
$node = $xml.SelectSingleNode("//msb:Project/msb:PropertyGroup/msb:ActiveDebugProfile", $nsMgr)
if ($node) {
# Set the device name
$node.InnerText = $NewProfileValue
# Then force it to physical device. If VS is open, it should do this anyways, but just in case.
$node2 = $xml.SelectSingleNode("//msb:Project/msb:PropertyGroup/msb:SelectedPlatformGroup", $nsMgr)
if ($node2) {
$node2.InnerText = "PhysicalDevice"
}
# Save back to file with UTF-8 encoding
$utf8 = New-Object System.Text.UTF8Encoding($false)
$writer = New-Object System.IO.StreamWriter($fullPath, $false, $utf8)
$xml.Save($writer)
$writer.Close()
Write-Host "ActiveDebugProfile updated to '$NewProfileValue'" in $csprojUserFile
}
else {
Write-Host "ActiveDebugProfile element not found in the XML."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment