locked
Update file content using powershell RRS feed

  • Question

  • I have a .conf file. The file has the following lines:

    ABCD

    EFGH

    XX

    XYZ

    I tried the following powershell command but didn't work and I think the reason is it is a .conf file not a .txt file

    (get-content C:\outputs.conf) | foreach-object {$_ -replace "XX", "Hello"} | set-content C:\outputs.conf

    Can anyone please help? Is there a command I can use to change the XX string to Hello?

    Monday, March 7, 2016 4:32 AM

All replies

  • Sorry children but we cannot solve your problems. You may need to learn how to ask a real question first.

    A ".conf" file is a ".conf" file.  Only very special "conf-igurators" can edit this kind of file. I recommend that all who have to ask this question shoul go back to elementary school and ask you teachers to help you understand this. Remember that this is spelled "C" "O" "M' "P" "U" "T" "E" "R".  Practice this so that you will not forget it.

    I understand that kids born in Tasmania can communicate with computers from birth.  Maybe they can help.

    I recommend that you find someone to help you.   We can't.  We can only wonder.

    'you  are wrong about XX because you have not learned XX yet'  -replace 'XX','goobers'


    \_(ツ)_/

    Monday, March 7, 2016 5:17 AM
  • You are really a weirdo. Its wasting other people's time reading your comment. It seems to me that you have problem understanding questions. My 8 year old nephew was able to understand it, you might have some serious problems.

    The solution is to rename the file to .txt, then use the following command to replace the word,

    (get-content C:\outputs.txt) | foreach-object {$_ -replace "XX", "Hello"} | set-content C:\outputs.txt

    Rename the file to .conf.

    Monday, March 7, 2016 9:43 PM
  • There uds absolutely n need t rename the file.

    $file=Get-Content C:\outputs.conf
    $file -replace 'XX','YY' | Out-File C:\outputs.conf

    After you have used Windows for a while you will learn that files with a txt extension show up as "txt Document" and files with unknown show up as just "CONF File" .

    This is because of how Windows associates files with programs.  This can be changed as needed.  None of it changes the fact that you already have a text file and can just edit.

    Excuse my poking a little fun but it always amuses me when a new Windows user makes technical sounding statements based on nothing.  I guess much of this comes from the TV shows about computer forensics which are mostly fantasy.

    Anyway.  You learned a little bit about both PowerShell and Windows.


    \_(ツ)_/



    • Edited by jrv Monday, March 7, 2016 9:56 PM
    Monday, March 7, 2016 9:55 PM
  • I'd like to confirm that my time was indeed wasted by jrv's post.
    Tuesday, August 22, 2017 9:49 PM
  • I'd like to confirm that my time was indeed wasted by jrv's post.

    And you learned nothing?  I guess failing is also wasting time.

    \_(ツ)_/

    Tuesday, August 22, 2017 10:23 PM
  • To make things a little bit more targed-aimed: If you ask questions like this one, it would be nice to explain a little bit more what happend. Was  there an error massage? Or did it run without error but simply nothing happend? 

    Next thing is own debugging. Is the first part of the pipeline

    (get-content C:\temp\outputs.conf) | foreach-object {$_ -replace "XX", "Hello"} 

    really working? Do you get your expected output there? If not, there could be a problem with the file itself, with the path or with the replace statement (always remember, that replace uses RegularExpressions, which can cause trouble to unexperienced users). If it is working, the problem is the output.

    Simply saying, that it's not working is not helpful.

    By the way, I tried it and can't reproduce your issue. It's working on my system.

    Furthermore, jrv is right that file extensions only assign programs to files. You can rename a txt-file to bmp, xlsx, jpg, abc, yourName, yourNeighborsName or any other extension you can imagine. But it is still a txt file and you can read it; only double-clicking the file won't work right anymore. On the other hand, you can rename a bmp file to txt, but that won't make it readable as text.

    Best regards

    Christoph


    • Edited by hpotsirhc Wednesday, August 23, 2017 6:01 AM
    Wednesday, August 23, 2017 5:55 AM
  • Hi,

    @Beau Beau : jrv is not a weirdo, he is right. The best way to learn is to fail. Why ? Because you have to go deeper in the "how it works".

    From a PowerShell syntax point of view, there is a faster solution :

    #requires -Version 1.0
    $file = "$env:SystemDrive\temp\outputs.conf"
    $content = (Get-Content -Path $file -ReadCount 0).Replace('XX','Hello')
    Set-Content -Path $file -Value $content -Encoding Unicode

    Try to avoid Out-*, which is slower than Set-*

    Try to avoid pipeline.

    Remark : It is just for perfomance optimization. (Altought, we all agree that C# still better) The result is the same. It could be interesting to know for bigger file. ^^


    The key of learning is practice.

    Thursday, August 24, 2017 10:45 PM