I'm writing a C# class that runs a Process (REG) to export registry keys. REG requires that you specify a filename to export to but I would rather have the output of REG directed to the standard output so I can capture it directly in my C# code (using Process.StandardOutput). Is there a way in PowerShell to specify the standard output as the filename?
+2
A:
If you have to use the REG program (rather than use PowerShell to query/dump the registry - or even just do it in the C# program itself), Probably the best you are going to get is to allow it to dump out to a temporary file, then pipe the contents of the file back to standard out and capture it in your C# program that way:
$guid = [Guid]::NewGuid().ToString("N")
REG EXPORT HKCU\Software\Microsoft\Windows "$env:temp\$guid" | Out-Null
Get-Content "$env:temp\$guid"
Remove-Item "$env:temp\$guid"
In case you were not aware: Using PowerShell, you can navigate the registry as though it were part of the file system. Perhaps this is helpful in some other regard?
cd HKCU:\Software\Microsoft\Windows
dir
Goyuix
2010-10-07 21:37:45
+1
A:
Just use 'CONOUT$' as the file name (as pojnted out in comments, this only works on Windows XP):
PS C:\> reg export HKLM\SOFTWARE\FileZilla 'CONOUT$'
■W i n d o w s R e g i s t r y E d i t o r V e r s i o n 5 . 0 0
[ H K E Y _ L O C A L _ M A C H I N E \ S O F T W A R E \ F i l e Z i l l a ]
" I n s t a l l _ D i r " = " C : \ \ P r o g r a m F i l e s \ \ F i l e Z i l l a "
" R u n i n S e c u r e M o d e " = " 0 "
" U s e R e g i s t r y " = " 0 "
" L a n g u a g e " = " E n g l i s h "
There are some UNICODE encoding issues in the output shown here, but you should be able to handle that in the buffer when you parse it.
zdan
2010-10-07 23:18:40
Interesting tip, but it did not work for me on Win7 or Server2003 (all I have available to test). I receive this error... ERROR: Unable to write to the file. There may be a disk or file system error.
JasonMArcher
2010-10-08 18:50:10
Also not working for me. I get the same message as above (ERROR: Unable to write to the file. There may be a disk or file system error).
threed
2010-10-08 21:00:40
When I tested on XP (SP3), it seems to work fine. Fails for me on Server2003 as well. It looks like Reg was updated which broke the trick.
zdan
2010-10-12 18:19:45