views:

23

answers:

3

I'm trying to use set-itemproperty to add an item to: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\Installation Sources

$InstallationSources = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup" -Name "Installation Sources"
$test = $InstallationSources."Installation Sources" + "C:\Test\I386"
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup" -Name "Installation Sources" "$test"

I can echo $test and it looks fine, several lines of paths with my addition at the end. But when I actually use set-itempproperty, it squishes everything into one line, which doesn't work. Each path needs to have its own line. Even manually added newlines aren't passed in (ie: "`nC:\Test\I386"). Ideas?

Thanks

A: 

$test is an array of strings and powershell is automatically joining them together for you when you say:

"$test"

You need to do the join yourself, providing the correct separator character. i.e.:

Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup" -Name "Installation Sources" ($test -join "`n")
zdan
A: 

If you want to preserve newlines then make sure the registry value is of type MultiString otherwise the registry won't allow the newlines AFAICT e.g.:

PS> New-ItemProperty hkcu:\ -Name bar -PropertyType MultiString
PS> Set-ItemProperty hkcu:\ -Name bar -Value "contents`r`nmore contents"
PS> Get-ItemProperty hkcu:\ -Name bar


PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\
PSParentPath :
PSChildName  : HKEY_CURRENT_USER
PSDrive      : HKCU
PSProvider   : Microsoft.PowerShell.Core\Registry
bar          : {contents
               more contents}
Keith Hill
A: 

I can't figure out how to reply or send a personal message to either of the people that replied, but first, Thanks, and second...

Keith: The key already exists and is of that type.

Zdan: Does't work. I've verified that your method collapse the array of strings into one string with newlines, but when I try to insert that into the registry, they still show up as one (actually two - I think because it was hitting a char limit) line.

lacheur
Then make sure you use "`r`n" to separate the strings.
Keith Hill
BTW, rather than add a new answer, it's better to `add comment` on the other anwers. You might want to delete this "answer.
Keith Hill