I would like to have a PowerShell script loop through all .xml files in a directory and delete a matching .xml node. I also want to save a copy of the original .xml file.
I have this line of code which is able to change one file at a time. However, I would like to have a script that does this for all .xml files in a folder.
Get-Content \\network\path\file1.xml | Where-Object {$_ -notmatch '<NUGLET key="000000000000025"/>'} | Set-Content \\network\path\file1.new.xml
I've been working on this script but I'm at a point where it seems to be looking in my documents directory instead of in the network path.
Get-ChildItem \\network\path\ *.xml |
ForEach-Object {
# Load the file's contents, delete string
(Get-Content $_) | Where-Object {$_ -notmatch '<NUGLET key="000000000000025"/>'}
}
So why am I getting the following error?
Get-Content : Cannot find path 'C:\Users\username\Documents\file1.xml' because it does not exist.
At C:\Users\username\Local\Temp\4a8c4fc2-9af6-4c35-ab40-99d88cf67a86.ps1:5 char:14
+ (Get-Content <<<< $_) | Where-Object {$_ -notmatch '<NUGLET key="000000000000025"/>'}
+ CategoryInfo : ObjectNotFound: (C:\Users\userna...-file1.xml:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
And how can I modify the script to make a copy of the original xml file for backup purposes.
EDIT:
So with Nate's suggestion I am now using the following:
Get-ChildItem \\network\path\ *.xml |
ForEach-Object {
# Load the file's contents, delete string
(Get-Content $_.fullname) | Where-Object {$_ -notmatch '<NUGLET key="000000000000025"/>' | Set-Content $_.fullname}
}