Pipe or output to a file

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5617

    by energy99 at 2013-03-02 21:47:22

    I'm running the following little script to get the file size or a specific file, this works great. I would like to pipe\output the results to a file but all I get is an empty file... below is the script...

    $filepath="d:\report.log"
    Get-ChildItem $filepath | ForEach-Object { Write-Host $_.name,==> ($_.Length/1MB).tostring("0.00")MB }

    by DonJ at 2013-03-02 22:36:46

    Use write-output. Never ever use write-host. You can't pipe from the host.

    by NachumElla at 2013-03-03 02:41:46

    Hello there!
    I want to reproduce your problem, but I cant understand what you're trying to achieve.
    You want to know the size of "d:\report.log"?

    Well, if you do, Here's a way (in bytes):
    "{0:N0}" -f (Get-ChildItem d:\report.log | select -ExpandProperty Length)
    Another way (KiloBytes):
    (Get-ChildItem d:\report.log).length / 1KB

    If it's not the case, I need more info.

You must be logged in to reply to this topic.