PowerShell Output to Text File

So I am fairly new to Powershell and need to create a script to rename the computers in our office. That portion of the script works. The part i am having trouble with is the output.

I have this set in task scheduler but when it runs i do not see if the rename was successful. Below is my script and below that is the what goes into the text file.

start-transcript -path C:\Users\abhagwandin.SENECA\Desktop\RenameResults.txt
$CSV = Import-Csv "C:\Users\abhagwandin.SENECA\Desktop\Computer Desktop Names Test.csv" -Header OldName, NewName
Foreach ($name in $CSV)


{
write-output $name

netdom renamecomputer $name.OldName /newname: $name.NewName /userd: admin /passwordd: pass /usero: admin /passwordo: pass /reboot /force
}



stop-transcript

Open in new window


Text File Output

-------------------------------------------------------------------------------
**********************
Windows PowerShell Transcript Start
Start time: 20150520154216
Username  :
Machine   :  (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\Users\abhagwandin.SENECA\Desktop\RenameRe
sults.txt
OldName                                 NewName                                
-------                                 -------                                
JFLAHNYCD1                              JFLAHERTY                              
**********************
Windows PowerShell Transcript End
End time: 20150520154218
**********************
yaknourAsked:
Who is Participating?
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.

SubsunCommented:
Try this code.. I have updated some basic reporting..
$CSV = Import-Csv "C:\Users\abhagwandin.SENECA\Desktop\Computer Desktop Names Test.csv" -Header OldName, NewName
$(Foreach ($name in $CSV)
{
netdom renamecomputer $name.OldName /newname: $name.NewName /userd: admin /passwordd: pass /usero: admin /passwordo: pass /reboot /force
	If ($?){
		Write-Output "$($name.OldName) renamed to $($name.NewName)"
	}Else{
		Write-Output "$($name.OldName) Failed to rename to $($name.NewName)"
	}
})| Out-File C:\temp\report.txt

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
Manojkumar RaneCommented:
Try this :

$CSV = Import-Csv "C:\Users\abhagwandin.SENECA\Desktop\Computer Desktop Names Test.csv" -Header OldName, NewName
$output = Foreach ($name in $CSV)


{
write-output $name

netdom renamecomputer $name.OldName /newname: $name.NewName /userd: admin /passwordd: pass /usero: admin /passwordo: pass /reboot /force
}

$output | Out-File C:\Users\manojkumar.rane\Desktop\output.csv
0
yaknourAuthor Commented:
Thank you, this is exactly what i was looking for.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.