Reading data from a file, using exclusion filters and parse them to Excel

Dear forum readers,

I like to make a software inventory per computer in a Microsoft Domain, but I also like to exclude some data from this collection (general information such as Microsoft, Intel drivers and so on).

In pseudo code, I like to achieve the following with my script:

  1. Read all computernames that are stored in a central text file c:\temp\computers.txt
  2. Make a software inventory per computer name that are mentioned in that central file → c:\temp\computers.txt
  3. Exclude keywords that are stored (per line) in an External file. For example: (c:\temp\File_with_to_be_excluded_keywords.txt
  4. Put all collected data in an Excel sheet (per tab the data of the collected information)

The script mentioned below works, but it doesn’t use the 2 files mentioned above as an input source.
It also does not add several tabs with the computername and the found software.

Please check the comments that I put in the script mentioned below (between de dashed line).
Thank you in advance for you help/support :slight_smile:

type or paste code here


Cls
#----------------------------------------------------------------------------------------------------------------------
# I would like to read a text file that will contains several computernames to start inventorizing software
# (on computername per line). Not using the array below
#----------------------------------------------------------------------------------------------------------------------
#  $computernames = "Computername1" # ,"computername2","computername3"
#----------------------------------------------------------------------------------------------------------------------
$array = @()
 
 foreach($computername in $computernames){

     $UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" #Define the variable to hold the location of Currently Installed Programs     
     $reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)  #Create an instance of the Registry Object and open the HKLM base key
     $regkey=$reg.OpenSubKey($UninstallKey) #Drill down into the Uninstall key using the OpenSubKey Method
     $subkeys=$regkey.GetSubKeyNames() #Retrieve an array of string that contain all the subkey names

     foreach($key in $subkeys){
         $thisKey=$UninstallKey+"\\"+$key 
         $thisSubKey=$reg.OpenSubKey($thisKey) 
         $obj = New-Object PSObject
         $obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
         

#----------------------------------------------------------------------------------------------------------------------
# I would like to use a file that contains keywords that will exclude information from the generated output file.
# For example: C:\temp\Exclude_This_Keywords_from_filter.txt
# Context of the file could be (per line a keyword) -> Intel, Microsoft, ...
# So not using the statement mentioned below
#----------------------------------------------------------------------------------------------------------------------
         if($obj -like "*Microsoft*" -or $obj -like "*Intel*" -or $obj -like "*Dell*" -or $obj -like "*Something else*"){}else{
         $array += $obj
         }
     } 
 }
 
 echo $array
#----------------------------------------------------------------------------------------------------------------------
# Last and least: I would like to add new tabs (Excel) that saves the computername as a tab and that includes all rows with the collected software inventory
#----------------------------------------------------------------------------------------------------------------------
 $array | Where-Object { $_.DisplayName } | select DisplayName   | ft -auto > c:\temp\$computername.csv

PowerShell_Student,
Welcome to the forum. :wave:t4:

You can (and you should) fix it. :wink: At the moment it is nearly impossible to distinct between text and code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

And what’s your actual question?

I’d recommend to ask only one question per thread. :wink:

The following help topics may already inspire you:

Use Get-Content to read a file and assign the output to the variable $computernames.

You can use a nested loop where you iterate over both lists and only output what matches you keywords.

You can use the great module from Doug Finke I already posted a link in my last answer. :+1:t4:

Format cmdlet like Format-Table should always be the last cmdlet in a pipeline as they are made to output pixels to the console - nothing else. It does not make sence to pipe their output to another cmdlet.
If you want to output your results to a CSV file you can use Export-Csv

Regardless of all that - it is recommended to format your code nicely to make it easier to read and not to use aliasses in scripts as they make the code harder to read.
You may read more about that topic here:

Hi Olaf,
Thank you for the hints, but when I do this, I don’t know where to put this line of code.
When I put the code in the the Foreach loop, I get multiple times the same results.
$computernames = Get-Content -Path c:\temp\computers.txt

I’d recommend to do a big step back and start with learning the fundamentals of scripting and programming in general and PowerShell in particular. That will save you from a lot of wasted time and frustrations.

So what other option would you think you have? :wink: And why would you place a variable assignment after the first use of this particular variable? :man_shrugging:t4:

I was not aware that you can put multiple lines into one variable in PowerShell.
Now I get it. Thank you very much!