Powershell - how do I add the servername to each result

Rob Murp 0 Reputation points
2023-04-13T09:22:08.7766667+00:00

Hi All, I am using the line below to check the version of a product installed on a list of servers. (input.txt has the list of servers) Get-WmiObject -computer (Get-Content "C:\input.txt") -Class win32_product | Select-Object -Property Name,Version, InstalledDate | where-object { $_.Name -like "uni*"} It works in so far as it gives me what I want - with output below
Name Version InstalledDate
However, how can I add the servername to each line it outputs?. (hope this makes sense?) with a big list of servers, I cant see at a glance which output line is from which server. Thanks in advance

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,463 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2023-04-13T14:53:33.4333333+00:00

    Add "PSComputerName" to the Select-Object cmdlet.

    You should know that querying that WMI class is very slow. It forces a verification of every item it touches.

    Using something like this in an Invoke-Command scriptblock is much faster:

    Get-ChildItem -Path  "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | Get-ItemProperty | Select-Object -Property DisplayName, Publisher, UninstallString, InstallLocation, InstallDate
    

  2. Rich Matheisen 45,906 Reputation points
    2023-04-14T15:33:41.39+00:00

    Using the registry to get the information:

    Invoke-Command -ComputerName ws07 -ScriptBlock {
        Get-ChildItem -Path  "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | 
            Get-ItemProperty | 
                Select-Object -Property DisplayName, DisplayVersion, InstallDate
    } | Where-Object {$_.DisplayName -like "*"} |
            Select-Object @{n='ComputerName';e={$_.PSComputerName}},DisplayName,DisplayVersion,@{n='InstallDate';e={$_.InstallDate -replace "^(\d\d\d\d)(\d\d)(\d\d)$", '$1-$2-$3'}}
    

    Here's what the results look like (used an '' instead of 'uni' in the code. Just change that to whatever you need):

    ComputerName DisplayName                                                   DisplayVersion   InstallDate
    ------------ -----------                                                   --------------   -----------
    ws07
    ws07         Defraggler                                                    2.22
    ws07         Microsoft 365 Apps for business - en-us                       16.0.16130.20332
    ws07
    ws07         Skype for Business Basic 2016 - en-us                         16.0.16130.20332
    ws07
    ws07         Intel(R) Management Engine Components                         1.0.0.0          2022-06-15
    
    0 comments No comments