Delete Line form text file

Welcome Forums General PowerShell Q&A Delete Line form text file

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
4 years, 7 months ago.

  • Author
    Posts
  • #14792

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

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

  • #14806

    Participant
    Points: 0
    Rank: Member

    Thankyou 🙂

  • #14850

    Participant
    Points: 0
    Rank: Member

    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

The topic ‘Delete Line form text file’ is closed to new replies.