Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

File I/O

Closed Thread
 
Thread Tools Display Modes
Old 10-11-2006   #1 (permalink)
MKielman
Guest


 

File I/O

All,

What is the best way to write output to a file rather then the terminal?
I have played around with set-content and add-content but neither of
them meet my needs. If i use set-content, then each new line that I
write to the file, overwrites the previous line, if I use add-content,
then each time I run the script, it appends it to the file if it already
exists. Is there a way I can create a handle to a file and specify once
what I plan to do with that file so that each time I run the script I
can be sure that I am not appending to a file that already exists unless
that is what I want to do?

Thanks,
Old 10-11-2006   #2 (permalink)
Brandon Shell
Guest


 

Re: File I/O

Unless I misunderstand you this should do what your looking for:

Command | out-file "c:\logfile.txt" -append


"MKielman" <mkielman@newsgroups.nospam> wrote in message
news:uGYGBYY7GHA.4064@TK2MSFTNGP03.phx.gbl...
> All,
>
> What is the best way to write output to a file rather then the terminal? I
> have played around with set-content and add-content but neither of them
> meet my needs. If i use set-content, then each new line that I write to
> the file, overwrites the previous line, if I use add-content, then each
> time I run the script, it appends it to the file if it already exists. Is
> there a way I can create a handle to a file and specify once what I plan
> to do with that file so that each time I run the script I can be sure that
> I am not appending to a file that already exists unless that is what I
> want to do?
>
> Thanks,


Old 10-11-2006   #3 (permalink)
Jim Holbach
Guest


 

RE: File I/O

I like functions, so here's one possible approach using a function:

function do-something {
param ($filepath,[switch]$overwrite, $scriptblock)

if ($Overwrite -and (test-path $filepath)) {clear-content $filepath}
&$scriptblock | add-content $filepath
}

---
Jim Holbach


"MKielman" wrote:

>
> What is the best way to write output to a file rather then the terminal?


Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scheduled task (run .bat file) fails every time with 0x80070002 (file not found) Ben Dilts Vista General 2 07-22-2008 08:50 AM
Create New folder, move file, delete file. Explorer Freeze erod49 General Discussion 10 05-20-2008 05:38 AM
file icons dont match contents-file names hidden Fosbury Vista file management 0 05-07-2008 05:48 PM
coping file from a remote file share - FILE IS NO LONG THERE bogus error message Heith Vista networking & sharing 0 10-18-2007 03:58 PM
File Open and File Save As Dialog Freeze and Hang All Applications =?Utf-8?B?TGlnaHRmb290ZQ==?= Vista file management 0 07-12-2006 11:39 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50