Get-Random from a Document, turn it into variable, then delete it, resave

Essentially want to have a document (random.txt) that will have a list of values in it. It will grab the random line, store it in a variable, display it, then remove it (by re-saving the document appears to be the best method I’ve found).

Random.txt 
1 
2 
3 
4

#--------------------------------------------------

$Number = Get-Random -InputObject (get-content C:\Scripts\random.txt)
$Number
# HERE IS WHERE I'M STUCK  - the below doesn't work, but it's similar to what I've seen in places online, including this forum #
get-content "C:\Scripts\random.txt" | where { $_ -ne "$number" } | Out-File "C:\Scripts\random.txt"

#
Random.txt (New Values Left)
1
2
4

# ---------------------------------------

Rinse and repeat.

My method removes everything. I’ve tried permutations of it, and it doesn’t help.

Hello ALombardi01,

This is expected behavior when you have Get-Content and Out-file in same pipeline.

You have two options:

  1. Output result to different file
  2. Get-Content of the file and save it in variable and then Out-file it to same file.
Reference: https://rakhesh.com/powershell/get-content-and-out-file-in-the-same-pipeline/

Hope that helps.