Brendan
Brendan

Reputation: 21

Powershell outputting info into text file but not into output pane

got a script here which I require the results that export into the text file to also show in the output pane at the bottom. Can anyone help please?

The results I've currently got is that it only shows the Make and model of the machine but not the others from Domain all the way to memory left in GB. I want it all to show on the output pane at the bottom and also to save in a text file which saves and opens straight away.

Please note: The file opens with the data in but the main issue is that it doesn't show in the output pane. Here is the script:

Clear-Host
Write-Host "Setting Execution Policy to Remote Signed..." -ForegroundColor Yellow 
Set-ExecutionPolicy RemoteSigned -Force
Write-Host "Your execution policy is set to:" -ForegroundColor Cyan 
Get-ExecutionPolicy
Start-Sleep -s 3
Clear-Host
Write-Host "Generating computer statistics..." -ForegroundColor Yellow
Write-Host " "
Start-Sleep -s 2
function systemstats {
Write-Host "Manufacturer:" -ForegroundColor Cyan 
Write-Host "$($m.Manufacturer)" -ForegroundColor Yellow
Write-Host "Model:" -ForegroundColor Cyan
Write-Host "$($m.Model)" -ForegroundColor Yellow
Write-Host "Domain:" -ForegroundColor Cyan
$env:USERDOMAIN
Write-Host "Computer Name:" -ForegroundColor Cyan 
$env:COMPUTERNAME
Write-Host "Operating System & Location:" -ForegroundColor Cyan 
(Get-WmiObject Win32_OperatingSystem).name
Write-Host "OS Architecture:" -ForegroundColor Cyan
if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -eq "64-bit")
{
    Write "64-bit OS"
}
else
{
    Write "32-bit OS"
}
Write-Host "OS Build:" -ForegroundColor Cyan 
(Get-CimInstance Win32_OperatingSystem).version
Write-Host "Version:" -ForegroundColor Cyan 
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseID).ReleaseID
Write-Host "Current IP Address:" -ForegroundColor Cyan
Ipconfig | Select-String IPv4
Write-Host "Calculating RAM installed in MB:" -ForegroundColor Cyan 
(systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim()
$m = (Get-WmiObject -Class Win32_ComputerSystem -Property * |
Select-Object -Property Manufacturer, Model)
Write-Host "Disk Space in GB:" -ForegroundColor Cyan
gwmi win32_logicaldisk | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
}
$result=(systemstats)
$result | Out-File "C:\Users\brendan.hargate\Desktop\test.txt"
Invoke-Item "C:\Users\brendan.hargate\Desktop\test.txt"

Upvotes: 0

Views: 373

Answers (2)

mklement0
mklement0

Reputation: 437363

Your systemstats function in part uses Write-Host which operates outside of PowerShell's (success) output stream.

Write-Host output cannot be captured (PSv4) / is by default not captured (PSv5+) in variables or output files. As the name suggests, Write-Host writes to the host UI (the console), and is not meant to output data - that's what Write-Output and its alias, Write, as well as implicit output (e.g., $env:USERDOMAIN by itself) are for.

A variable assignment such as $result = ... only captures (success) output-stream output, i.e., the implicit output and the output from the Write (a.k.a. Write-Output) command. By contrast, your Write-Host commands printed straight to the console.

Given that you sent $result to a file - without also printing it to the console - the net effect was that only the Write-Host output appeared in the console.

The solution therefore has two components:

  • Modify your function to only use implicit output (Write-Output output) in order to produce data output.

  • Then use Tee-Object to print to both the console (via the success output stream) and a file, as suggested in Mathias R. Jessen's helpful answer.


As an aside, in PSv5+, you could get away with the following (although it is ill-advised), based on the ability to capture Write-Host output - now a virtual alias of Write-Information - via the newly introduced output stream number 6:

systemstats 6>&1 | # PSv5+: redirect the information stream to the (success) output stream.
  Tee-Object -FilePath "$([Environment]::GetFolderPath('Desktop'))\text.txt"

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Use Tee-Object instead of Out-File:

$result | Tee-Object -FilePath "C:\Users\brendan.hargate\Desktop\test.txt"

Tee-Object (inspired by tee), will duplicate the input stream - one copy is passed on down the pipeline (which in your case will end up in the command pane), the other is written to a variable or a file (like in the above example)

Upvotes: 1

Related Questions