Again with the Output RRS feed

  • Question

  • Result displays on console instead of Excel sheet..Can someone show me the light>??

    $erroractionpreference = "SilentlyContinue" 
    # Create a New Excel Object for storing Data 
    $a = New-Object -comobject Excel.Application 
    $a.visible = $True  
    $b = $a.Workbooks.Add() 
    $c = $b.Worksheets.Item(1) 
    # Create the title row 
    $c.Cells.Item(1,1) = "Machine Name" 
    $c.Cells.Item(1,2) = "Model" 
    $c.Cells.Item(1,3) = "Manufacturer" 
    $d = $c.UsedRange 
    $d.Interior.ColorIndex = 23 
    $d.Font.ColorIndex = 2 
    $d.Font.Bold = $True 
    $intRow = 2 
    $colComputers = get-content C:\temp\PCs.txt
    # Run through the Array of Computers 
    foreach ($strComputer in $colComputers) 

    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
    # Get System Info 

    $colOS =Get-WmiObject -class Win32_OperatingSystem -computername $Strcomputer 
    foreach($objComp in $colOS) 
        try {
            $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $env:computername)
     'MODEL:{0}  Manufactured by:"{1}"' -f $regkey.GetValue('Model'),$regkey.GetValue('Manufacturer')

      "end of script"
    $intRow = $intRow + 1 

    $intRow = $intRow + 1 
    # Save workbook data 
    # Quit Excel (Remove "#" if you want to quit Excel after the script is completed)

    Tuesday, May 27, 2014 5:48 PM


All replies

  • Your script is confusing, because you look like you want to retrieve information from a remote computer and even use Get-WMIObject, but then you open the local computer's registry.

    This is probably a bit easier:

    get-content "C:\temp\PCs.txt" | foreach-object {
      get-wmiobject Win32_ComputerSystem -computername $_ | select-object __SERVER,Manufacturer,Model
    } | export-csv "C:\Temp\ComputerInfo.csv" -notypeinformation

    Then just open C:\Temp\ComputerInfo.csv in Excel.

    -- Bill Stewart [Bill_Stewart]

    • Edited by Bill_StewartModerator Tuesday, May 27, 2014 9:14 PM Forgot WMI class name (Win32_ComputerSystem)
    Tuesday, May 27, 2014 5:54 PM
  • Sorry about that. I am trying to read a value from a remote registry (list of computers) and output it into Excel

    $strMachineName = import-csv C:\temp\PCS.csv
    foreach ($line in $strMachineName)
        try {
            $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $env:computername)
     'MODEL:{0}  Manufactured by:"{1}"' -f $regkey.GetValue('Model'),$regkey.GetValue('Manufacturer')
    Tuesday, May 27, 2014 6:04 PM
  • I guess I would be repeating myself if I said you're opening the local computer's registry, not the remote computer's.

    It is simpler to use WMI and export to CSV, like I showed in my post. Excel can open CSV files directly.

    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, May 27, 2014 9:48 PM
    Tuesday, May 27, 2014 6:17 PM
  • This is funny.  I wrote and posted that pieces of code a year or more ago.  It has popped up twice in two days.  The same error.  Eveyone copies it and leaves the dummy value in just like the original.  "$env:COMPUTERNAME"  which always points to the local machine.

    The interesting lesson all should take away is that you should never attempt to use a piece of code you do not understand.  It will nearly always be embarrassing.


    Tuesday, May 27, 2014 9:48 PM
  • The ($env:computername) -variable you are referring to in the following line is the local machine.

    [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $($env:computername))

    Does the content of your txt-file have a header? Ex:


    then try

    [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Line.Computer)

    If your txt-file only contains one machinename per line and no headers, then replace your Import-CSVwith Get-Content and try

    [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Line)
    Thursday, May 29, 2014 5:59 PM
  • I am trying to start the remote registry service for each PC to fetch a value from the registry but going nowhere. 

    $Computers = import-csv C:\temp\PCs.csv
    $results=foreach ($Computer in $Computers) {
    (Get-WmiObject -computerName $Computers Win32_Service -Filter "Name='RemoteRegistry'").StartService() | Out-Null         
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer.Computer)
              New-Object PsObject -Property @{
    ComputerName = $                                                                          
        (Get-WmiObject -computerName $Computers Win32_Service -Filter "Name='RemoteRegistry'").StopService() | Out-Null                                                                      }
    $results|Export-Csv C:\temp\PCsResults.csv -NoType

    Friday, May 30, 2014 4:22 PM
  • You have to start remote registry and open the firewall.  It is easier to just use WMI registry to retrieve a value.

    To retrieve a registry value with WMI you do not need to start the remote registry service.


    Friday, May 30, 2014 4:37 PM
  • Here is an Example:



    • Edited by jrv Friday, May 30, 2014 4:45 PM
    • Marked as answer by Newbie 1 Friday, May 30, 2014 5:46 PM
    Friday, May 30, 2014 4:44 PM