locked
Open, Write and Close the file using Powershell RRS feed

  • Question

  • Hi All,

    I need to create/open a file and write some data into it and close that file.

    Could anyone please help me with this.

    Thanks.

    Thursday, October 4, 2012 10:24 AM

Answers

  • You don't need to explicitly create, open, or close a file in Powershell.  Here are some ways to write to a file, in addition to New-Item:

    $text = 'Hello World'
    
    # Create file:
    
    $text | Set-Content 'file.txt'
    #or
    $text | Out-File 'file.txt'
    #or
    $text > 'file.txt'
    
    # Append to file:
    
    $text | Add-Content 'file.txt'
    #or
    $text | Out-File 'file.txt' -Append
    #or
    $text >> 'file.txt'


    Grant Ward, a.k.a. Bigteddy

    Thursday, October 4, 2012 6:44 PM

All replies

  • $path = '[your path here]'
    $file = '[your TXT filename here].txt'
    $file2 = '[your CSV filename here].csv'

    New-Item -path $path -Name $file -Value 'Test of creating a new file using the New-Item cmdlet. So there!!!' -ItemType file -force
    New-Item -path $path -Name $file2 -Value 'Test of creating a new file using the New-Item cmdlet. So there!!!' -ItemType file -force
    # get-help new-item -Examples

     


    Thanks, Steve

    Thursday, October 4, 2012 6:03 PM
  • You don't need to explicitly create, open, or close a file in Powershell.  Here are some ways to write to a file, in addition to New-Item:

    $text = 'Hello World'
    
    # Create file:
    
    $text | Set-Content 'file.txt'
    #or
    $text | Out-File 'file.txt'
    #or
    $text > 'file.txt'
    
    # Append to file:
    
    $text | Add-Content 'file.txt'
    #or
    $text | Out-File 'file.txt' -Append
    #or
    $text >> 'file.txt'


    Grant Ward, a.k.a. Bigteddy

    Thursday, October 4, 2012 6:44 PM
  • Actually, Powershell ISE will keep a file open after your script runs until you close the ISE.  I know because I am running a script right now which creates a file and then emails it as an attachment.  But I can not delete the file in Explorer because " . . . file is open in Windows PowerShell ISE."  Also, the PS script can not overwrite the file because the SMTP portion of the PS script will not close the file.  There may be a way to close the file, but it is unknown to me, which is how I came across this post.
    Friday, November 8, 2013 4:27 PM
  • If you're using the SmtpClient and MailMessage classes directly (instead of using the built-in Send-MailMessage cmdlet), that's probably the culprit.  You have to call Dispose() on the MailMessage object when you're done, or it leaves handles open to any attached files.

    Friday, November 8, 2013 4:32 PM
  • Actually, Powershell ISE will keep a file open after your script runs until you close the ISE.....There may be a way to close the file, but it is unknown to me, which is how I came across this post.

    I may have found a way... 

    $file = Get-ChildItem -LiteralPath $path $stream = $file.OpenRead() ... $stream.Close()

    It's the .close() part at the end that makes all the difference.
    Tuesday, July 29, 2014 3:50 PM
  • To open a file read an close it just do this:

    $lines=Get-Content somefile.txt

    The file does not remain open.

    The original problems has nothing to do with opening files.

    if you still have issues please start a new question.


    ¯\_(ツ)_/¯

    Tuesday, July 29, 2014 4:41 PM
  • You don't need to explicitly create, open, or close a file in Powershell.  Here are some ways to write to a file, in addition to New-Item:

    $text = 'Hello World'
    
    # Create file:
    
    $text | Set-Content 'file.txt'
    #or
    $text | Out-File 'file.txt'
    #or
    $text > 'file.txt'
    
    # Append to file:
    
    $text | Add-Content 'file.txt'
    #or
    $text | Out-File 'file.txt' -Append
    #or
    $text >> 'file.txt'


    Grant Ward, a.k.a. Bigteddy

    The problem with Out-File is that it works like the console. When text is output, it will be truncated if it is wider than the default. So if you are looking for something where you need to store lots of long strings, then you may want to choose a different route, or ensure than you specify a width for your output.

    http://technet.microsoft.com/en-us/library/ee176924.aspx

    For example to get 200 characters of width:

    $text | Out-File 'myfile.txt' -width 200

    I do not know for certain if there is a limit to how wide it can be. I also assume the default is the standard console width (80 characters I believe.)

    Wednesday, October 1, 2014 2:58 PM
  • Scott - question closed 3 years ago.  The answer was noted.  If you have a question please start a new topic.

    I will also note that youmisunderstand the article posted.  In the case in this topic the width is not an issue.  It only effects how console formatted output is handled.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, October 1, 2014 3:16 PM
    • Proposed as answer by ririon Monday, February 5, 2018 1:39 PM
    Wednesday, October 1, 2014 3:13 PM
  • Generate file with line of 10000 Xs.

    PS C:\scripts> 'X'*10000|out-file xxex.txt

    Now read file and get length of written line:

    PS C:\scripts> cat xxex.txt|%{$_.Length}
    10000

    See - no limit when output comes from a variable.  -Width only manages conversion buffers for output from 'Format" commands.  Console output uses a default formatter that truncates long strings.  Variable output does not need this.


    ¯\_(ツ)_/¯

    Wednesday, October 1, 2014 3:21 PM
  • Hi All,

    I need to create/open a file and write some data into it and close that file.

    Could anyone please help me with this.

    Thanks.

    In my case, for files generated in PS, say ipconfig /all >ipconfig.txt in a directory at C:/ps is there a cmdlet that will let me open this txt file in the windows default text editor?

    I tried simply entering PS C:\ps> ipconfig.txt but it throws a not recognized error as follows:

    ipconfig.txt : The term 'ipconfig.txt' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At line:1 char:1+ ipconfig.txt+ ~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (ipconfig.txt:String) [], CommandNotFoundException    + FullyQualifiedErrorId : CommandNotFoundException
    Appreciate your insights as always.

    Tuesday, December 3, 2019 7:07 PM
  • Hi All,

    I need to create/open a file and write some data into it and close that file.

    Could anyone please help me with this.

    Thanks.

    In my case, for files generated in PS, say ipconfig /all >ipconfig.txt in a directory at C:/ps is there a cmdlet that will let me open this txt file in the windows default text editor?

    I tried simply entering PS C:\ps> ipconfig.txt but it throws a not recognized error as follows:

    ipconfig.txt : The term 'ipconfig.txt' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At line:1 char:1+ ipconfig.txt+ ~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (ipconfig.txt:String) [], CommandNotFoundException    + FullyQualifiedErrorId : CommandNotFoundException
    Appreciate your insights as always.

    figured it out:

    Open notepad:

    PS> notepad

    Open a file in notepad in the current dir:

    PS C:\ps> notepad ipconfig.txt


    Tuesday, December 3, 2019 7:10 PM