PowerShell: how to use standard output with file name

I am writing a C # class that starts a process (REG) to export registry keys. REG requires you to specify the file name for export, but I would prefer that the REG output be directed to standard output, so I can commit it directly in my C # code (using Process.StandardOutput). Is there a way in PowerShell to specify standard output as the file name?

+3
source share
2 answers

If you need to use the REG program (instead of using PowerShell to query / reset the registry - or even just do it in the C # program itself), probably the best thing you are going to get is to let it unload into a temporary file, then drag the contents of the file back to the standard version and write it to your C # program this 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"

If you did not know: using PowerShell, you can navigate the registry as if it were part of the file system. Perhaps this is useful in some other way?

cd HKCU:\Software\Microsoft\Windows
dir
+4
source

Just use "CONOUT $" as the file name (as mentioned in the 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 "

UNICODE, .

+1

Source: https://habr.com/ru/post/1768498/


All Articles