Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting Results To File

Status
Not open for further replies.

wotsit

MIS
Oct 18, 2002
47
GB
Hi,

I'm having a bit of trouble with a script i'm running.

The script successfully queries computers for a list of their installed software and displays the results in the powershell window.

I need to be able to write the results to a file instead and i'm a bit stuck on how to, is anybody able to help me and point me in the right direction please?

In a .vbs script i used to put something like this at the top of the code:

strOutFile = "C:\scripts\out.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.CreateTextFile(strOutFile, True)

and make sure the required data line started objOutFile.Write "..."

but i'm not sure how to do it in powershell.

The code i'm using is this:

$COMPUTERS=IMPORT-CSV C:\scripts\serverlist.txt

FOREACH ($PC in $COMPUTERS) {
$computername=$PC.NameOfComputer

# Branch of the Registry
$Branch='LocalMachine'

# Main Sub Branch to open
$SubBranch="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

$registry=[microsoft.win32.registrykey]::OpenRemoteBaseKey('Localmachine',$computername)
$registrykey=$registry.OpenSubKey($Subbranch)
$SubKeys=$registrykey.GetSubKeyNames()

# Obtain software details

Foreach ($key in $subkeys)
{
$exactkey=$key
$NewSubKey=$SubBranch+"\\"+$exactkey
$ReadUninstall=$registry.OpenSubKey($NewSubKey)
$Value=$ReadUninstall.GetValue("DisplayName")
WRITE-HOST $computername, $Value
}
}


I know the write-host command is the i need to amend, but i'm not sure what to amend it to.

Any help would be greatly appreciated.

Thanks,
H

 
Here's one way to do it. The extra variable $outstring helps keep the output to one line for each entry
Code:
    $Value=$ReadUninstall.GetValue("DisplayName")
    $outstring = "$computername,$value"
    out-file -filepath c:\path\filename.txt -encoding ascii -append -inputobject $outstring
 
Thank you for your help Crobin1.

I've amended the script with your suggestion, but unfortunately nothing happens. As soon as the script is started, it stops. There are no error messages and the output file is not created.

The amended script is below, have I missed something?

$COMPUTERS=IMPORT-CSV C:\scripts\serverlist.txt

FOREACH ($PC in $COMPUTERS) {
$computername=$PC.NameOfComputer

# Branch of the Registry
$Branch='LocalMachine'

# Main Sub Branch to open
$SubBranch="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

$registry=[microsoft.win32.registrykey]::OpenRemoteBaseKey('Localmachine',$computername)
$registrykey=$registry.OpenSubKey($Subbranch)
$SubKeys=$registrykey.GetSubKeyNames()

# Obtain software details

Foreach ($key in $subkeys)
{
$exactkey=$key
$NewSubKey=$SubBranch+"\\"+$exactkey
$ReadUninstall=$registry.OpenSubKey($NewSubKey)
$Value=$ReadUninstall.GetValue("DisplayName")
$outstring = "$computername,$value"
out-file -filepath c:\scripts\output.txt -encoding ascii -append -inputobject $outstring
}
}


Thanks,
H
 
You're not really making a Powershell script here, this is much more of a VBscript posing as Powershell, for one.

For two, you can use Powershell's builtin Get-WmiObject cmdlet to query all Windows Installer (MSI) installed applications. This is a pretty powerful and simple way to get the information you're looking for.

Get-WmiObject -Class Win32_Product | select Name | Sort

will give you an alphabetized listing of nearly all of the software installed on a particular PC. You can use Get-WmiObject's -Computername Parameter to pass arguments from the pipeline, if you have an array of computers. For instance:

Get-QADComputer -memberof "testcomputers" |
Get-WmiObject -Class Win32_Product | select Name | Sort

to get a list of all SW installed on all the PC's of the testcomputers Active Directory Security Group.
 
Wotsit, I ran your script exactly as shown in your second post and it worked just fine for me. Did you run it exactly (same directory, same files, same account with permissions to access remote registry) as before?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top