Delete Line form text file

This topic contains 9 replies, has 4 voices, and was last updated by Profile photo of Bob McCoy Bob McCoy 3 years, 2 months ago.

  • Author
    Posts
  • #14792
    Profile photo of Perry Jones
    Perry Jones
    Participant

    So I'm writing a script to generate a list of random passwords. Once the list is generated they are output to a .txt file. They are in a very simple list like so:
    pass1
    pass2
    pass3
    pass4

    nothing else.
    What I need to happen is for a separate script to read this file, grab the first line and push the password to active directory. Once it's read the password and its all set it should delete what it read from the text file. So then it should read as such:

    pass2
    pass3
    pass4

    So this raises a few questions. First of all can I read and delete the line to begin with. Secondly if I delete that line, will it leave empty space? Third, If it leaves empty space can that space be deleted so that the second line then becomes the first line? and what commands/code would be used to accomplish this?

    Any insight to this would be incredibly helpful. Note: This isn't anything major and I don't expect anyone to jump on it immediately as I have plenty of time to do this.

  • #14793
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Get-Content source.txt | where { $_ -ne 'pass1' } | Out-File dest.txt

    You're not "deleting" a line – you're reading in the lines, discarding the one you don't want, and writing out the remaining lines. So, no empty space.

  • #14794
    Profile photo of Perry Jones
    Perry Jones
    Participant

    I almost forgot to mention all the code necessary for changing the password in AD is already written. All I need is a snippet that will read a file in this location:
    C:\Scripts\FuturePass.txt
    grab the first line pass it into a variable called $SunPass to be set as the password in AD.

    • #14795
      Profile photo of Perry Jones
      Perry Jones
      Participant

      Ah I see. so here is my question. These two scripts will run perpetually can the "dest.txt" be the same file as the source?
      In other words can I pull passwords from "FuturePass.txt" and then overwrite "FuturePass.txt" with the new version that excludes the password that was used?

  • #14796
    Profile photo of Don Jones
    Don Jones
    Keymaster

    You'd have to read the entire file into memory first.

    $lines = Get-Content source.txt
    $first = $lines[0]
    $lines | where { $_ -ne $first } | out-file source.txt

    Probably a hundred ways to do it. That's one.

  • #14797
    Profile photo of Perry Jones
    Perry Jones
    Participant

    Holy S****! If there is a god you are him! Thank you Thank you! This should work. Us jones' gotta help each other out, right?

    Cheers

  • #14801
    Profile photo of Perry Jones
    Perry Jones
    Participant

    One last question. Not that big of a deal, but I have no clue what "-ne" does...it kind of looks like it could be "Not Equals" or something but I have no clue. Just curious..my code is all done anyhow.

  • #14802
    Profile photo of deiandrei
    deiandrei
    Participant

    Yes, it means "not equal". You can find a list of comparison operators by typing "help about_comparison_operators".

  • #14806
    Profile photo of Perry Jones
    Perry Jones
    Participant

    Thankyou 🙂

  • #14850
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Here's another variation ...

    #1..100 | foreach { "pass$psItem" } >> source.txt
    $pass = Get-Content .\source.txt -TotalCount 1
    $newList = Get-Content .\source.txt | select -Skip 1
    $newList | Out-File .\source.txt

You must be logged in to reply to this topic.