Getting Powershell Output to a File

Hello Experts,

I have a Powershell script that displays the output I want on the screen, but I'm not sure how to send it to a text or csv file as well.  I'm sure it is an easy thing to do, I'm just drawing a blank.  Here is my code:

$DaysInactive = 90
$Time = (Get-Date).Adddays(-($DaysInactive))
$DeadComputers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | select name
foreach ($i in $DeadComputers)
{
if (Test-Connection -ComputerName $i.name -BufferSize 16 -Count 1 -Quiet) {Write-Host $i.name is online}
else {Write-Host $i.name is offline}
}

Thanks in advanced for your help.
LVL 1
ndalmolin_13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
Add a new property to your select results, then set it in your foreach loop.

Export those results to CSV  using Export-CSV.

Ex.
$DaysInactive = 90
$Time = (Get-Date).Adddays(-($DaysInactive)) 
$DeadComputers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | select name,status

foreach ($i in $DeadComputers)
{
if (Test-Connection -ComputerName $i.name -BufferSize 16 -Count 1 -Quiet) {
    Write-Host $i.name is online
    $i.Status = "Online"
    }
else {
    Write-Host $i.name is offline
    $i.Status = "Offline"
    }
}

$DeadComputers | Export-CSV -Path "C:\test\deadcomputers.csv" -NoTypeInformation

Open in new window

2
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Here you are,

Don'tuse "Write-Host" except when you want the text to go to the console, and no-where else.

Putting text in quotes will print it normally.

(You can also use Write-Output "Text" but if you omit write-output it automatically assumes quoted text with no other commands is to be written using that method)

$DaysInactive = 90
$Time = (Get-Date).Adddays(-($DaysInactive)) 
$DeadComputers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | select name
$Results=foreach ($i in $DeadComputers){
	if (Test-Connection -ComputerName $i.name -BufferSize 16 -Count 1 -Quiet) {
		"$($i.name) is online"
	} else {
		"$($i.name) is offline"
	}
}
# Export to a regular text File
$Results | Out-File "C:\Admin\Output.txt"
# Export to a CSV
$Results | Export-CSV"C:\Admin\Output.csv"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ndalmolin_13Author Commented:
Thanks
0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.