Escaping and eliminating spaces from PowerShell scripts to enable roaming of file type associations on Windows 10 -


solved: solution below after ************'s

i trying output similar preferably using out-file.

<?xml version="1.0" encoding="utf-8"?> <defaultassociations>     <association identifier=".html" progid="firefoxhtml" applicationname="chromehtml" /> </defaultassociations> 

the problem having write-host, spaces being introduced along variable values. use out-file instead, when try concatenate each line this:

    $out = $out + '<association identifier="'$($item.psparentpath | split-path -leaf)'"'     out-file -filepath c:\temp\test.xml $out 

i error

unexpected token '$(' in expression or statement.

i'm close getting desired output.

if permissions changed on %windir%\system32\oemdefaultassociations.xml allow standard users write, login script can put copy of script outputs file per user file type associations can roam between windows 10 computers.

$userchoicekeys = get-childitem "hkcu:\software\microsoft\windows\currentversion\explorer\fileexts\*\userchoice" $userchoicekeys = $userchoicekeys + (get-childitem "hkcu:\software\microsoft\windows\shell\associations\urlassociations\*\userchoice") write-host '<?xml version="1.0" encoding="utf-8"?>' write-host '<defaultassociations>' foreach ($item in $userchoicekeys) { write-host -nonewline '<association identifier="'$($item.psparentpath | split-path -leaf)'"' $progid = $(get-itemproperty $item.pspath -name "progid").progid write-host -nonewline ' progid="'($progid) $applicationkey = "hklm:\software\classes\"+$progid+"\application" $applicationname =  $(get-itemproperty $applicationkey -name "applicationname" -erroraction silentlycontinue).applicationname write-host -nonewline '" applicationname="'$applicationname'" />' write-host '' } write-host -nonewline '</defaultassociations>' 

***************solution******************

$customassociationsfilepath = "$($env:userprofile)\customassociations.xml"  $userchoicekeys = get-childitem "hkcu:\software\microsoft\windows\currentversion\explorer\fileexts\*\userchoice"  $userchoicekeys += get-childitem "hkcu:\software\microsoft\windows\shell\associations\urlassociations\*\userchoice" $nl = [environment]::newline  $outputdata = '<?xml version="1.0" encoding="utf-8"?>' + $nl + '<defaultassociations>'  foreach ($item in $userchoicekeys)  {      $outputdata += $nl + '<association identifier="' + $($item.psparentpath | split-path -leaf) + '"'      $progid = $(get-itemproperty $item.pspath -name "progid").progid      $outputdata += ' progid="' + ($progid)      $applicationkey = "hklm:\software\classes\" + $progid + "\application"      $applicationname =  $(get-itemproperty $applicationkey -name "applicationname" -erroraction silentlycontinue).applicationname      $outputdata += '" applicationname="' + $applicationname + '" />'  }  $outputdata += $nl + '</defaultassociations>'  out-file -encoding utf8 -inputobject $outputdata -filepath $customassociationsfilepath 


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -