Delete Line form text file

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:

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:


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.

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.

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:
grab the first line pass it into a variable called $SunPass to be set as the password in AD.

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?

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.

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?


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.

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

Thankyou :slight_smile:

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