Windows Vista Forums

writing Textfiles
  1. #1


    Jens Schulze Guest

    writing Textfiles

    hello,



    has anybody a sample how to write Textfiles with the PS? ich wish to create
    a file, that is not the problem:

    $datei=New-Item -ItemType "file" -Path "C:\Test2\" -Name
    "protokoll.txt" -force

    with "$datei | Get-Member" i found that ehr ist es Method AppendText, but
    how to use them to append a new line at the end of the text-file?

    thank you for help, Jens





      My System SpecsSystem Spec

  2. #2


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    RE: writing Textfiles

    "Jens Schulze" wrote:

    > with "$datei | Get-Member" i found that ehr ist es Method AppendText, but
    > how to use them to append a new line at the end of the text-file?


    As you can see from the output of Get-Member, the method AppendText returns
    an
    System.IO.StreamWriter object. You can assign that to a variable:

    PS> $sw = $datei.AppendText()

    You can then see the methods of the StreamWriter by piping it to Get-Member
    again:

    PS> $sw | gm

    You can write to the StreamWriter using it's methods Write() or WriteLine():

    PS> $sw.Write("Ich lerne PowerShell.")

    When you're finished you can call the Flush() method to save the changes:

    PS> $sw.Flush()

    To check the content of the file use Get-Content:

    PS> Get-Content $datei

    An even shorter solution is to pipe strings to Out-File cmdlet which also
    has an append parameter:

    "Test" | Out-File -filePath $datei -append

    And you can use shell redirection operators:

    "Test" > $datei # overwrite
    "Test" >> $datei # append

    --
    greetings
    dreeschkind

      My System SpecsSystem Spec

  3. #3


    Jens Schulze Guest

    Re: writing Textfiles

    thank you
    my fault was, that i wrote

    $sw = $datei.AppendText

    instead of

    $sw = $datei.AppendText()

    with the "()" it works fine.

    thank you, jens

    "dreeschkind" <dreeschkind@discussions.microsoft.com> schrieb im Newsbeitrag
    news:3BA1DABF-2487-4D54-9B2E-F35EC5B07F8D@microsoft.com...
    > "Jens Schulze" wrote:
    >
    >> with "$datei | Get-Member" i found that ehr ist es Method AppendText, but
    >> how to use them to append a new line at the end of the text-file?

    >
    > As you can see from the output of Get-Member, the method AppendText
    > returns
    > an
    > System.IO.StreamWriter object. You can assign that to a variable:
    >
    > PS> $sw = $datei.AppendText()
    >
    > You can then see the methods of the StreamWriter by piping it to
    > Get-Member
    > again:
    >
    > PS> $sw | gm
    >
    > You can write to the StreamWriter using it's methods Write() or
    > WriteLine():
    >
    > PS> $sw.Write("Ich lerne PowerShell.")
    >
    > When you're finished you can call the Flush() method to save the changes:
    >
    > PS> $sw.Flush()
    >
    > To check the content of the file use Get-Content:
    >
    > PS> Get-Content $datei
    >
    > An even shorter solution is to pipe strings to Out-File cmdlet which also
    > has an append parameter:
    >
    > "Test" | Out-File -filePath $datei -append
    >
    > And you can use shell redirection operators:
    >
    > "Test" > $datei # overwrite
    > "Test" >> $datei # append
    >
    > --
    > greetings
    > dreeschkind




      My System SpecsSystem Spec

  4. #4


    Andrew Watt [MVP] Guest

    Re: writing Textfiles

    Another approach, using PowerShell cmdlets, is as follows:

    PS C:\PowerShellScripts> set-content -path Test3.txt -value "Hello
    world!"
    PS C:\PowerShellScripts> get-content -path Test3.txt
    Hello world!
    PS C:\PowerShellScripts> add-content -path Test3.txt -value "`n"
    PS C:\PowerShellScripts> get-content -path Test3.txt
    Hello world!


    PS C:\PowerShellScripts>

    Notice that extra whitespace (more than one line it seems) is added by
    the add-content cmdlet.

    Andrew Watt MVP

    On Wed, 27 Sep 2006 09:14:01 -0700, dreeschkind
    <dreeschkind@discussions.microsoft.com> wrote:

    >"Jens Schulze" wrote:
    >
    >> with "$datei | Get-Member" i found that ehr ist es Method AppendText, but
    >> how to use them to append a new line at the end of the text-file?

    >
    >As you can see from the output of Get-Member, the method AppendText returns
    >an
    >System.IO.StreamWriter object. You can assign that to a variable:
    >
    >PS> $sw = $datei.AppendText()
    >
    >You can then see the methods of the StreamWriter by piping it to Get-Member
    >again:
    >
    >PS> $sw | gm
    >
    >You can write to the StreamWriter using it's methods Write() or WriteLine():
    >
    >PS> $sw.Write("Ich lerne PowerShell.")
    >
    >When you're finished you can call the Flush() method to save the changes:
    >
    >PS> $sw.Flush()
    >
    >To check the content of the file use Get-Content:
    >
    >PS> Get-Content $datei
    >
    >An even shorter solution is to pipe strings to Out-File cmdlet which also
    >has an append parameter:
    >
    >"Test" | Out-File -filePath $datei -append
    >
    >And you can use shell redirection operators:
    >
    >"Test" > $datei # overwrite
    >"Test" >> $datei # append


      My System SpecsSystem Spec

writing Textfiles problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: split textfile to more textfiles Eric VB Script 9 20 Aug 2009
Need to get tricky with textfiles - banging my head repeatedly joe.hurzeler PowerShell 5 12 Dec 2008
Writing to CD-R annieuk Vista General 8 22 Aug 2007
Writing to DVD+RW Tom Darlington Vista General 0 21 Mar 2007
CD / DVD Writing Dave Vista hardware & devices 3 20 Feb 2007