Powershell: Shuffle-words / Generate Random words / Mix words Puzzle

Suzana Eree 811 Reputation points
2021-05-17T14:26:12.407+00:00

hello, I need to make a shuffle-words just like this one: https://onlinerandomtools.com/shuffle-words

I have a .txt file with some different words, which I have to mix, just like a puzzle. So I made a PowerShell script, but it doesn't make any change on my file.

$resultsdir = "C:\Folder1\"
Get-ChildItem -Path $sourcedir -Filter *.txt | ForEach-Object {
$words = Get-Content  -Path $_.FullName -Raw -Encoding UTF8 | Sort-Object {Get-Random} -unique | select -first 100

Function Create-Words
{ 
  # query the smaller list of words for single entry (2 times)
  $word1 = $words | Sort-Object {Get-Random} -unique | select -first 1  
  $word2 = $words | Sort-Object {Get-Random} -unique | select -first 1  

  # create randomwords
  $random = Get-Random

  # mix and return new random words
  return (Get-Culture).TextInfo.ToTitleCase($word1) + (Get-Culture).TextInfo.ToTitleCase($word2) + $random

}

for($i=1; $i -le 20; $i++){

}

$Allines = $_ -replace '\s+(.*?)','\x20' #put all words on the same line  FIND: \s+(.*?) REPLACE BY: \x20
 $words -replace $Allines | Out-File -FilePath 

}
Windows for business Windows Server User experience PowerShell
Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Suzana Eree 811 Reputation points
    2021-05-18T13:48:30.227+00:00

    @Anonymous @Andreas Baumgarten @Chris

    DONE. The code of mr. @Anonymous had a small problem on Split. Instead of Split(",. ") I change it to Split(" ") Works beautiful now.

    I hope that also the code of @Andreas Baumgarten to be updates, so as to work as well.

    $file = "C:\Folder1\file.txt"  
     $newfile = "C:\Folder1\newfile.txt"  
     $content = (Get-Content -Path $file -Encoding UTF8) -join ''  
     [System.Collections.ArrayList]$words = $content.Trim(",. ").Split(" ")  
     $counter = $words.count  
     $newwords = @()  
     for($i=0; $i -lt $counter; $i++){  
        $randomword = Get-Random -InputObject $words  
        $newwords += $randomword  
        $words.Remove($randomword)  
     }  
     $newwords -join ' ' | Out-File -FilePath $newfile  
    
    0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Suzana Eree 811 Reputation points
    2021-05-18T15:41:56.683+00:00

    yes, nice question. actually I am using this TOOL every day, in the morning. Before starting to work, I need to warm my mind with something. Other people read things, I am playing puzzle. By the way, I am an architect.

    So, I want to have a backup solution for that website, in case will disappear on internet. I didn't find anything much better on internet. Now, with a powershell code, like yours or @Anonymous I will be able to play my puzzle game, even If I am on the plane, or if I don't have internet in some locations.

    I like playing games before doing art !

    0 comments No comments

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-05-19T09:04:05.137+00:00

    Hi @Suzana Eree ,

    here the script with the do...until. I got lost in the different versions of the scripts in this threads, So I am still using my last version.
    The script below will count the words in file and run the Create-Words again and again until the word count is reached. For instance 200 words in the file means the Create-Word function is executed 200 times. At the end the result will be saved in the new file.

    # Define file name  
    $fileName = "./Junk/1/Wordlist.txt"  
    $newfilename = "./Junk/1/NewWordlist.txt"  
    # Get content of file  
    $content = Get-Content -Path $fileName -Raw -Encoding UTF8  
    $words = ((($content.Split(" ")).Replace(".","")).Replace(",","")).Replace("`n","")  
    $words.count  
    #Define function  
    Function Create-Words {  
        $script:word1 = Get-Random -InputObject $words # get first random word from word list  
        $script:word2 = Get-Random -InputObject $words  # get second random word from word list  
        Write-Host "First word:  " $script:word1 -ForegroundColor Green # write output word1  
        Write-Host "Second word: " $script:word2 -ForegroundColor Magenta # write output of word2  
    }  
    # Execute function (x times / depending on the word count)  
    # Get the number of words in file  
    $replaceCount  = $words.count  
    # Reset counter  
    $i = 0  
    # Start the "Do...Until"   
    do {  
        Create-Words  
    # Replace random word1 with random word2 and increase the counter  
    $content = $content.Replace("$word1", "$word2")   
    $i++  
    } until ($i -eq $replaceCount)  
    # Write the new file  
    $content | Out-File -FilePath "$newfilename" -force  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  3. Suzana Eree 811 Reputation points
    2021-05-19T09:47:45.773+00:00

    that would be enough to do the job. Just run this code on Powershell

    ((Get-Content -Path C:\Folder1\file.txt -Raw ) -split "\s+" | 
        Sort-Object {Get-Random} ) -join ' ' |
            Out-File -FilePath C:\Folder1\NewFile.txt
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.