@Olaf I change a code with your code. And the OUTPUT is ok, It genarates a random words from the original file. So thank you.
The small problem is that I believe I didn’t replace very correctly your code. I get an error on powershell, even if the output is fine after running the code.
$fileName = "C:\Folder1\file.txt"
# Get content of file
$content = ((Get-Content -Path C:\Folder1\file.txt -Raw ) -split "\s+" |
Sort-Object {Get-Random} ) -join ' ' |
$words = (((($content.Split(" ")).Replace(".","")).Replace(",","")).Replace("`n",""))
#Define function
Function Create-Words {
$script:word1 = Get-Random -InputObject $words
$script:word2 = Get-Random -InputObject $words
Write-Host "First word: " $script:word1 -ForegroundColor Red # output word1
Write-Host "Second word: " $script:word2 -ForegroundColor Cyan # output word2
}
# Execute function
Create-Words
# Replace words randomly
((Get-Content -Path C:\Folder1\file.txt -Raw ) -split "\s+" |
Sort-Object {Get-Random} ) -join ' ' |
Out-File -FilePath C:\Folder1\NewFile.txt