Updating values in XML

Hi,
I am trying to update a value in XML file. I can get to that property as below, please help;

PS C:> ($xml2.unattend.settings.component).registeredorganization
xxxx Group Headquarters, IT Services

But when I try to update the value of that property with below command it gives me error ;

PS C:> $XMLTest= ($xml2.unattend.settings.component).registeredorganization
PS C:> $XMLTest.RegisteredOrganization = $registeredOrganization
The property ‘RegisteredOrganization’ cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1

Thanks,
Kishor

There is not a sample of your XML, but looking at what you have, you have already included “registeredorganization” in your variable and there is probably not a registeredorganization property of registeredorganization like you are trying to set

$XMLTest= ($xml2.unattend.settings.component).registeredorganization

So putting RegisteredOrganization you are basically looking for this in your xmls
($xml2.unattend.settings.component).registeredorganization.RegisteredOrganization = $registeredOrganization

You likely just need to drop the second RegisteredOrganization

PS C:\> $XMLTest= ($xml2.unattend.settings.component).registeredorganization
PS C:\> $XMLTest = $registeredOrganization

Hi Curtis,
I found the xml I am working with has a namespace.What do I need to do in this case?
Below is the snap of my XML file, I need to update the value for ‘RegisteredOrganization’;

Thanks,
Kishor

Hey Kishor, you will likely have to use a gist to post your xml. This forum tends to strip it out.

mistake post. Please ignore

Hi Curtis,
Below is link for my xml;

Thanks,
Kishor

There are some issues with your xml which I will assume are type-os in the provided example and not in your actual file. The issue you are having with setting your value is that you have multiple components that match.

IE.
$xml2.unattend.settings.component will match the first component in the file where name is blank, and it will match the second component in the file where name is “Microsoft-Windows-”. The result is that at this point you have a collection of object that .registeredorganization is looking in, and you cannot set the value like this across a collection.

What you need to do is select the specific component you need so that you have just the single object when you are trying to set the registeredorganization value.

Results

xxxx Group Headquarters, IT Services
PowerShell.org

Thanks a lot Curtis! your solution has solved my problem. Now I am able to get to the value without worrying about the namespaces involved in the XML file.

Thanks,
Kishor