Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
  • In terms of character encoding, Set-Content defaults to system's default code page. When in doubt, use an explicit encoding, such as -Encode UTF8.

  • The above assumes that the XML element in question is on a single line. If it isn't, things get more complicated: while you can use Get-Content -Raw (PowerShell v3+) to read the entire input file at once, Set-Content will add an extra newline to the output; also, you'd have to adapt the regex passed to -replace above to account for line-spanning whitespace.

  • In terms of character encoding, Set-Content defaults to system's default code page. When in doubt, use an explicit encoding, such as -Encode UTF8.

  • The above assumes that the XML element in question is on a single line. If it isn't, things get more complicated: while you can use Get-Content -Raw (PowerShell v3+) to read the entire input file at once, Set-Content will add an extra newline to the output; also, you'd have to adapt the regex passed to -replace above to account for line-spanning whitespace.

    • Matt's helpful answer shows a more robust way of handling the value substitution, using proper XML parsing via the [xml] type (fully qualified name: System.Xml.XmlDocument).
  • In terms of character encoding, Set-Content defaults to system's default code page. When in doubt, use an explicit encoding, such as -Encode UTF8.

  • The above assumes that the XML element in question is on a single line. If it isn't, things get more complicated: while you can use Get-Content -Raw (PowerShell v3+) to read the entire input file at once, Set-Content will add an extra newline to the output; also, you'd have to adapt the regex passed to -replace above to account for line-spanning whitespace.

    • Matt's helpful answer shows a more robust way of handling the value substitution, using proper XML parsing via the [xml] type (fully qualified name: System.Xml.XmlDocument).
added 18 characters in body
Source Link
mklement0
  • 451.7k
  • 68
  • 726
  • 986

First, I'd recommend doing it all in PowerShell, which ultimately simplifies matters:
The complexity of dealing with 2 very different environments aside, invoking PowerShell commands with powershell -ccommand and a command string containing the command to execute introduces quoting complexities that can be tricky to resolve.

Note the need to use a single-line literal to pass to powershell.exe -ccommand, which makes the whole thing unreadable; also, adding -noprofile is generally a good idea so as to suppress loading of the profile file, which is intended only for interactive use.

 powershell -noprofile -ccommand "$files = Get-ChildItem $env:AppData\Skype -Recurse config.xml; foreach($file in $files) { (Get-Content $file.fullname) -replace '<(AdvertPlaceholder)>1</\1>', '<$1>0</$1>' | Set-Content $file.fullname }"

First, I'd recommend doing it all in PowerShell, which ultimately simplifies matters:
The complexity of dealing with 2 very different environments aside, invoking PowerShell commands with powershell -c and a command string containing the command to execute introduces quoting complexities that can be tricky to resolve.

Note the need to use a single-line literal to pass to powershell.exe -c, which makes the whole thing unreadable; also, adding -noprofile is generally a good idea so as to suppress loading of the profile file, which is intended only for interactive use.

 powershell -noprofile -c "$files = Get-ChildItem $env:AppData\Skype -Recurse config.xml; foreach($file in $files) { (Get-Content $file.fullname) -replace '<(AdvertPlaceholder)>1</\1>', '<$1>0</$1>' | Set-Content $file.fullname }"

First, I'd recommend doing it all in PowerShell, which ultimately simplifies matters:
The complexity of dealing with 2 very different environments aside, invoking PowerShell commands with powershell -command and a command string containing the command to execute introduces quoting complexities that can be tricky to resolve.

Note the need to use a single-line literal to pass to powershell.exe -command, which makes the whole thing unreadable; also, adding -noprofile is generally a good idea so as to suppress loading of the profile file, which is intended only for interactive use.

 powershell -noprofile -command "$files = Get-ChildItem $env:AppData\Skype -Recurse config.xml; foreach($file in $files) { (Get-Content $file.fullname) -replace '<(AdvertPlaceholder)>1</\1>', '<$1>0</$1>' | Set-Content $file.fullname }"
added 7 characters in body
Source Link
mklement0
  • 451.7k
  • 68
  • 726
  • 986
  • pass a file's filepath through
  • while also transforming that file's content.

Your attempt muddles the two task: it passes in file objects (that resolve to their paths in a string context), then mistakenly treats them as if they were their contents, then magically expects the modified contents to turn back into file objects (paths) telling Set-Content what files to write back to.

  • pass a file through
  • while also transforming that file's content.

Your attempt muddles the two task: it passes in file objects, then mistakenly treats them as if they were their contents, then magically expects the modified contents to turn back into file objects telling Set-Content what files to write back to.

  • pass a file's path through
  • while also transforming that file's content.

Your attempt muddles the two task: it passes in file objects (that resolve to their paths in a string context), then mistakenly treats them as if they were their contents, then magically expects the modified contents to turn back into file objects (paths) telling Set-Content what files to write back to.

added 108 characters in body
Source Link
mklement0
  • 451.7k
  • 68
  • 726
  • 986
Loading
added 108 characters in body
Source Link
mklement0
  • 451.7k
  • 68
  • 726
  • 986
Loading
Source Link
mklement0
  • 451.7k
  • 68
  • 726
  • 986
Loading