2

I am having an issue with exporting results to a txt in Powershell. I have 2 commands I need to run and when they are done, I am hoping to get something like this:

Name                                            Version         
----                                            -------         
Mitel Connect                                   214.100.1252.0  
Cylance Unified Agent                           2.4.1070.1      

Instead of this:

Name                                            Version         
----                                            -------         
Mitel Connect                                   214.100.1252.0  

Name                                            Version         
----                                            -------         
Cylance Unified Agent                           2.4.1070.1  

Here is the code:

get-package 'Microsoft Edge','Mitel Connect', '*7-Zip*','*Cylance*','*Office Profession*','*Dameware*','Cisco AnyConnect Secure Mobility Client'  | select name,version | Out-File "C:\temp\export.txt" -Append
Get-WmiObject -Class Win32_Product | where Name -eq "Manageengine AssetExplorer Agent" | select Name, Version | Out-File "C:\temp\export.txt" -Append

I have tried piping the code, google and nothing works,

Does anyone have any idea who to get the output?

1
  • As an aside: The CIM cmdlets (e.g., Get-CimInstance) superseded the WMI cmdlets (e.g., Get-WmiObject) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even have them anymore. Note that WMI still underlies the CIM cmdlets, however. For more information, see this answer. Commented Jul 22, 2022 at 14:29

2 Answers 2

2

If the output file only needs to record the output from these two commands:

  • Call (& ) them in sequence via a script block ({ ... })...
  • ... pipe the output to a single Select-Object call ...
  • ... which you can then pipe to a single Out-File call.
& {
  Get-Package 'Microsoft Edge','Mitel Connect', '*7-Zip*','*Cylance*','*Office Profession*','*Dameware*','Cisco AnyConnect Secure Mobility Client' 
  Get-CimInstance -Class Win32_Product | Where-Object Name -eq "Manageengine AssetExplorer Agent"
} |
  Select-Object Name, Version |
  Out-File "C:\temp\export.txt"

That way, the output objects are formatted together, as a single table, by the single Out-File call.

(By contrast, if you call Out-File multiple times, you'll get a separate table in the file for each call, which is what you saw.)

Note, however, that the resulting file format is only meant for display purposes, as it uses the same rich formatting you'd see in the console (terminal) by default, which is meant for the human observer rather than programmatic processing.

For programmatic processing, consider a structured text format, such as CSV (Export-Csv), which also allows you to append to the file iteratively later.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you only get the name and version of these objects, pushing them into an array and then exporting the array would work as you intend it to. However, in case you try to add more properties in each select, this will not work. The objects in the array will preserve the structure of the 1st element added to it.

This is the code that managed to create the desired result.

$arr = @()

get-package 'Microsoft Edge' | select name, version | % { $arr += $_ }
Get-WmiObject -Class Win32_Product | where Name -eq "Go Programming Language amd64 go1.18" | select Name, Version |  % { $arr += $_ }

$arr | Out-File "C:\temp\export.txt" -Append

The following 2 snippets show the undesired effect if you try to add properties in the select that don't exist in both structures.

$arr = @()

Get-WmiObject -Class Win32_Product | where Name -eq "Go Programming Language amd64 go1.18" | select Name, Vendor, Version |  % { $arr += $_ }
get-package 'Microsoft Edge' | select name,version | % { $arr += $_ }


$arr | Out-File "C:\temp\export.txt" -Append

This will have an empty Vendor prop for the "get-package" objects

$arr = @()

get-package 'Microsoft Edge' | select name,version | % { $arr += $_ }
Get-WmiObject -Class Win32_Product | where Name -eq "Go Programming Language amd64 go1.18" | select Name, Vendor, Version |  % { $arr += $_ }

$arr | Out-File "C:\temp\export.txt" -Append

This will disregard the "Vendor" property for all data from "Get-WmiObject" Command

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.