3

Is it possible to import registry entries directly from the output of another process (i.e. over a pipe from STDIN)?

I have some registry entries in a .reg file that I'd like to import, but only after making some on-the-fly modifications (filling in placeholder values like username, etc.). I was planning on using the methods from How can you find and replace text in a file using the Windows command-line environment? to do the modifications, but now I need to figure out how to get the modified version into the registry.

(gc foo.reg) -creplace '^"UserName"=""$', "`"UserName`"=`"$env:USERNAME`"" | | ...

I've been looking at the documentation for REG IMPORT, which requires a filename as an arguments. In Unix shell-scripting, which I have much more experience in, there are a lot of ways to solve the problem. If we imagine a Unix version of the command reg -i which requires a filename, some possible solutions would be

sed 's/bar/baz/' foo.reg|reg -i

or

sed 's/bar/baz/' foo.reg|reg -i -

or

sed 's/bar/baz/' foo.reg|reg -i /dev/stdin

or even

reg -i <(sed 's/bar/baz/' foo.reg)

(Only the first two are at all natural.)

Is there an equivalent for Windows?

Note that I'm fine with solutions in either CMD- or PowerShell-style scripting, if that makes a difference.

Community
  • 1
  • 1
Aaron Davies
  • 1,190
  • 1
  • 11
  • 17

1 Answers1

4

reg import expects a filename and not stdin AFAICT from its help. So I would read the original .reg file and modify the contents, save that to a new file and import that new file:

(Get-Content foo.reg) -replace '^("UserName"\s*=\s*")"$', "`$1$env:UserName`"" > modfoo.reg
reg import modfoo.reg
Keith Hill
  • 194,368
  • 42
  • 353
  • 369