I'm not able to do that, i just want the output .txt with one line and each output followed by ;
New-Item C:\temp\test.txt
Set-Content C:\temp\test.txt (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))})
Add-Content C:\temp\test.txt (Get-CimInstance -ClassName Win32_Processor).name
Add-Content C:\temp\test.txt (Get-WmiObject Win32_OperatingSystem).caption
Add-Content C:\temp\test.txt (Get-CimInstance -ClassName Win32_VideoController).name
CodePudding user response:
You can write the outputs to an array and use -join
to write the array elements with custom separator
mkdir C:\temp
@((Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))}),
(Get-CimInstance -ClassName Win32_Processor).name,
(Get-WmiObject Win32_OperatingSystem).caption,
(Get-CimInstance -ClassName Win32_VideoController).name
) -join ';' > C:\temp\test.txt
Note that Get-CimInstance -ClassName Win32_VideoController
will return information for all GPUs in the system, so if you have multiple GPUs (for example hybrid graphics systems) then it'll print multiple lines instead of one. So you may need to use (Get-CimInstance -ClassName Win32_VideoController).name -join ','
instead, or (Get-CimInstance -ClassName Win32_VideoController)[0].name
if you just want the first graphics card
I guess the same thing happens in Get-CimInstance -ClassName Win32_Processor
if there are multiple physical CPUs in the system
CodePudding user response:
You would need to use the -NoNewLine
switch for both, Set-Content
and Add-Content
combined with string concatenation following what you already have:
$file = New-Item test.txt
Get-CimInstance Win32_PhysicalMemory |
Measure-Object -Property Capacity -Sum |
ForEach-Object {"{0:N2}" -f ([math]::Round(($_.Sum / 1GB),2))} |
Set-Content -LiteralPath $file -NoNewline
';' (Get-CimInstance -ClassName Win32_Processor).Name | Add-Content -LiteralPath $file -NoNewline
';' (Get-CimInstance Win32_OperatingSystem).Caption | Add-Content -LiteralPath $file -NoNewline
';' (Get-CimInstance -ClassName Win32_VideoController).Name | Add-Content -LiteralPath $file
However, it's worth noting that it would be a lot easier to just concatenate all output from the cmdlets in memory and output to the file just once, for this example we can use the -f
format operator:
$file = New-Item test.txt
'{0:N2};{1};{2};{3}' -f
[math]::Round((((Get-CimInstance Win32_PhysicalMemory).Capacity | Measure-Object -Sum).Sum / 1Gb), 2),
(Get-CimInstance -ClassName Win32_Processor).Name,
(Get-CimInstance Win32_OperatingSystem).Caption,
(Get-CimInstance -ClassName Win32_VideoController).Name | Set-Content -LiteralPath $file