Home > other >  Powershell output new line
Powershell output new line

Time:07-23

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?

CodePudding user response:

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.

CodePudding user response:

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

  • Related