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. Chris 656 Reputation points
    2021-05-18T05:33:23.457+00:00

    Andreas,

    I am new in Powershell. What is the difference between

    $script = get-random .... and
    $script:word1 = get-random

    Chris


  2. Suzana Eree 811 Reputation points
    2021-05-18T05:39:43.44+00:00

  3. Anonymous
    2021-05-18T08:20:35.183+00:00

    Hi,

    Please check to see if this works.

    $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  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. Suzana Eree 811 Reputation points
    2021-05-18T14:24:46.68+00:00

    @Andreas Baumgarten I realized that your powershell code didn't work because I have empty spaces at the beginning of the text. Right now, your code seems to be almost good, it change the words, but not too many as in the script of @Anonymous . I believe something must be change in the $words line, in the replacement.

     $fileName = "C:\Folder1\file.txt"  
     $newfilename = "C:\Folder1\newfile.txt"  
     # Get content of file  
     $content = Get-Content -Path $fileName -Raw -Encoding UTF8  
     $words = (((($content.Split(" ")).Replace(".","")).Replace(",","")).Replace("`n",""))  
     #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  
     Create-Words  
     # Replace random word1 with random word2 and write file  
     $content.Replace("$word1", "$word2") | Out-File -FilePath "$newfilename" -force  
    

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.