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

Welcome Forums General PowerShell Q&A Get-Random from a Document, turn it into variable, then delete it, resave

Viewing 1 reply thread
  • Author
    Posts
    • #244301
      Participant
      Topics: 6
      Replies: 13
      Points: 100
      Rank: Participant

      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).

      Rinse and repeat.

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

    • #247056
      Participant
      Topics: 1
      Replies: 59
      Points: 587
      Helping Hand
      Rank: Major Contributor

      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.

Viewing 1 reply thread
  • The topic ‘Get-Random from a Document, turn it into variable, then delete it, resave’ is closed to new replies.