Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

wotsit (MIS)
21 Oct 11 10:01
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

 
crobin1 (MIS)
26 Oct 11 10:06
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
 
wotsit (MIS)
27 Oct 11 5:56
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
1RedOne (Programmer)
14 Dec 11 9:28
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.
 
crobin1 (MIS)
15 Dec 11 9:27
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?

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close