1

I am using this question as my base reference Powershell - Query remote registry key value and generate text file IF value equals 1

I am trying to find the value of a registry key and output that to a file.

I modifed the code in that link to look like this.

$keyname = 'Software\Policies\Microsoft\Windows\WindowsUpdate\AU'

Import-Csv 'C:\computers.csv' | % {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",
   $_.machinename)
$key = $reg.OpenSubkey($keyname)
$value = $key.GetValue('AUoptions')
Write-Host ($_.machinename , $value) | write-output "c:\computerresults.csv"

The script works correctly until I get to the | write-output. I cannot get it to dump the results to a file. I tried export-csv as well.

Any assistance given would be greatly appericated.

3 Answers 3

2

Write-Host breaks the pipeline. Use Write-Output instead when you want to display information within a piped command.

Also, Write-Output is not the command you're looking for to export data. Look into Export-Csv and Out-File for file output.

Export-Csv

Out-File

Sign up to request clarification or add additional context in comments.

Comments

0

You are missing a closing curly brace to your % loop. I have inserted it, and modified the last line to give you the output you desire (pretty sure, not tested but it should work.

New-Object PSObject @{MachineName=$_.machinename;Value=$value} } | Export-CSV "c:\computerresults.csv" -NoTypeInfo

That will output a series of custom objects that have 2 values each, MachineName and Value. Then it takes all those objects and exports them to a CSV. The -NoTypeInfo is so that you don't get a line at the top stating what kind of object it was exported from.

1 Comment

If one of these answers resolves your question please select that answer as the solution to close out the question.
0

Thanks everyone. This is what I ended up using

$keyname = 'Software\Policies\Microsoft\Windows\WindowsUpdate\AU'

Import-Csv 'C:\computers.csv' | % {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",
       $_.machinename)
$key = $reg.OpenSubkey($keyname)
$value = $key.GetValue('AUoptions')
New-Object PSObject @{MachineName=$_.machinename;Value=$value} 
Write-Host ($_.machinename , $value)
} | out-file "c:\computerresults.csv" 

@TheMadTechnician. If I used the Export-csv (either with the write-host or without) I got this as my output IsReadOnly IsFixedSize IsSynchronized Keys Values SyncRoot Count FALSE FALSE FALSE System.Collections.Hashtable+KeyCollection System.Collections.Hashtable+ValueCollection System.Object 2

So I combined what both you and @kohlbrr provided and come up with a working solution that displayed the results to me on the screen as well as dropping them in the file.

Thanks again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.