# Integrate do .. until looping in Powershell

**URL:** https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558
**Category:** PowerShell Help
**Created:** [May 18, 2021, 3:22pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558 "2021-05-18T15:22:42Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 18, 2021, 3:22pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/1 "2021-05-18T15:22:42Z")

</div>

hello. My Powershell code must mix all words from `file.txt` My `Replace` doesn’t change too many words between them I believe I must Integrate a `do .. until looping` code. I find this [LINK](https://sid-500.com/2019/11/04/powershell-do-while-vs-do-until/) and I try many combinations, but ruins all my code.

```auto
$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 
      $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
  $content.Replace("$word1", "$word2") | Out-File -FilePath "$newfilename" -force

```

---

<div class="post-metadata">

### Author: ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)
#### Post date: [May 18, 2021, 5:50pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/2 "2021-05-18T17:50:47Z")

</div>

Is this a homework question?

---

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 18, 2021, 6:15pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/3 "2021-05-18T18:15:15Z")

</div>

something like that. I want to understand how can be integrate that function on a code. I’m not a developer. Step by step, optional.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 18, 2021, 6:19pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/4 "2021-05-18T18:19:36Z")

</div>

I’d start with removing the line numbers and all backtricks from the code. At the moment it would be a pain in the ass to copy your code and play with it. You force people willing to help you to make more effort than necessary. 😉

---

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 18, 2021, 6:36pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/5 "2021-05-18T18:36:26Z")

</div>

Of course. Done ! @Olaf

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 18, 2021, 7:40pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/6 "2021-05-18T19:40:54Z")

</div>

> [@nicu\_fantanaru](#):
>
> Of course. Done !

Cool. Thanks.

> [@nicu\_fantanaru](#):
>
> My Powershell code must mix all words from `file.txt`

Just that I get it right - you want to take all the words from a text file and mix them up in a random order, right?

---

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 18, 2021, 7:57pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/7 "2021-05-18T19:57:10Z")

</div>

@Olaf yes, exactly, it doesn’t matter how many words I have in the file. They must be as mixed as possible, like a very difficult puzzle 🙂

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 18, 2021, 9:32pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/8 "2021-05-18T21:32:52Z")

</div>

OK, I got it. 😉

Let’s try something. First I’m going to describe how I would approach it. You can try it yourself and if you get stuck with the code we do the code together. OK? 🙂

Since you actually want to create a new file anyway I’d drop all that replacing stuff.

Basically we need all the words in the file in a collection where we can randomly pick one of the words at a time, remove it from the source collection and add it to a new collection. We have to repeat this until all source words have been used.

If we have all the words in a new order in our new collection we can write it to a file. Simple, isn’t it? 😉

Let’s do it step by step. First - read the file and create a collection of all the words in it.

---

<div class="post-metadata">

### Author: ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)
#### Post date: [May 18, 2021, 9:59pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/9 "2021-05-18T21:59:32Z")

</div>

OK, just to be clear, if this a college assignment or homework question we try avoid assisting with those as the first port of call should be your tutor. However, you’ve had a decent crack at it with your code so some general observations:

It would be good to know what the input file looks like and what the expected output is. If it’s just a list of words, that’s super simple to solve. If it’s properly structured text (and your `-split` and `-replace` operations suggest that might be the case) then it’s a harder problem, particularly if you want to maintain some structure in the new file. What’s going to happen to all the a’s and i’s when you do a replace? What about words that appear more than once, do you want to replace both instances or just the first match? 🤔

So, if it’s just a list of words you won’t need a loop. Just take the list, **sort** it **randomly** (that’s a hint) and output it.

If it’s structured text and you want to maintain the structure, you probably will need a loop and your `replace` operations will need more complicated regular expressions.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 18, 2021, 10:19pm UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/10 "2021-05-18T22:19:33Z")

</div>

> [@matt-bloomfield](#):
>
> **sort** it **randomly** (that’s a hint)

I didn’t know that. 😲 Very cool. 👍🏽 Thanks. 😘 🖖🏽

---

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 19, 2021, 4:51am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/11 "2021-05-19T04:51:12Z")

</div>

thank you @Olaf and @matt-bloomfield

You both write literature. Not even a line of code. And just for future reference, you need to know that a man learns from concrete examples, not from words full of wisdom. Whoever talks to you a lot and doesn’t show you anything concrete, is the same man who doesn’t teach you anything.

If you had found the solution, it was easiest to write it down, and I would have learned how to do it.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 19, 2021, 8:06am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/12 "2021-05-19T08:06:45Z")

</div>

> [@nicu\_fantanaru](#):
>
> You both write literature. Not even a line of code. And just for future reference, you need to know that a man learns from concrete examples, not from words full of wisdom. Whoever talks to you a lot and doesn’t show you anything concrete, is the same man who doesn’t teach you anything.

I totally disagree.

> [@nicu\_fantanaru](#):
>
> If you had found the solution, it was easiest to write it down, and I would have learned how to do it.

And I don’t like to do other peoples homework on request. 😉

Regardless of that is it still unclear what exactly you actually want to do.

---

<div class="post-metadata">

### Author: ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)
#### Post date: [May 19, 2021, 8:51am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/13 "2021-05-19T08:51:19Z")

</div>

> [@nicu\_fantanaru](#):
>
> You both write literature. Not even a line of code. And just for future reference, you need to know that a man learns from concrete examples, not from words full of wisdom.

We can’t post code as a solution if we don’t know what the problem is.  
Show us what the input looks like and what the output should look like and we can help you work out a solution.

---

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 19, 2021, 9:05am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/14 "2021-05-19T09:05:50Z")

</div>

@matt-bloomfield @Olaf

For example I have a text with 400 words, but I show you a short example with 7 words.

**The input: ( file.txt )**

`We can’t post code as a solution if we don’t know what the problem is.`

**The output: ( newfile.txt )**

`as what the problem a solution we is. post if can’t We code know don’t`

---

<div class="post-metadata">

### Author: ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)
#### Post date: [May 19, 2021, 9:20am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/15 "2021-05-19T09:20:58Z")

</div>

OK, so although the input is not a list of words, you’re not maintaining the structure. In that case, I would suggest that you treat punctuation as part of the word and don’t bother stripping it out. You can then just treat all your words as though it is a list which can be randomized and then joined together with spaces. No loop necessary:

```auto
$sentence = "We can't post code as a solution if we don't know what the problem is."
$words = $sentence -split ' '
$newSentence = ($words | Sort-Object {Get-Random}) -join ' '
Write-Output $newSentence

```

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 19, 2021, 9:22am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/16 "2021-05-19T09:22:51Z")

</div>

> [@nicu\_fantanaru](#):
>
> For example I have a text with 400 words, but I show you a short example with 7 words.
> 
> **The input: ( file.txt )**
> 
> `We can’t post code as a solution if we don’t know what the problem is.`
> 
> **The output: ( newfile.txt )**
> 
> `as what the problem a solution we is. post if can’t We code know don’t`

That should have been in your initial question. 😉

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

```

---

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 19, 2021, 9:25am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/17 "2021-05-19T09:25:21Z")

</div>

yes, @matt-bloomfield , but I need to select the words from the file, not implicit on powershell 🙂

---

<div class="post-metadata">

### Author: ![nicu\_fantanaru](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@nicu\_fantanaru](https://forums.powershell.org/u/nicu_fantanaru)
#### Post date: [May 19, 2021, 9:31am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/18 "2021-05-19T09:31:27Z")

</div>

@Olaf I put your code on the beginning. Doesn’t change almost nothing in the Output.

```auto
((Get-Content -Path C:\Folder1\file.txt -Raw ) -split "\s+" | 
    Sort-Object {Get-Random} ) -join ' ' |
        Out-File -FilePath 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 
      $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
  $content.Replace("$word1", "$word2") | Out-File -FilePath "$newfilename" -force

```

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 19, 2021, 9:39am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/19 "2021-05-19T09:39:55Z")

</div>

> [@nicu\_fantanaru](#):
>
> I put your code on the beginning. Doesn’t change almost nothing in the Output.

We cannot help you when you don’t understand the help. With the given example text in the source file my code does exactly what you described. You don’t need more code.

I’d recommend to do a big step back and start with learning the very basics of PowerShell first. 😉 That’ll save you from a lot of wasted time and frustration.

---

<div class="post-metadata">

### Author: ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)
#### Post date: [May 19, 2021, 9:45am UTC](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558/20 "2021-05-19T09:45:48Z")

</div>

Sorry, I’m not going to provide a full code solution for a homework question. You now have all the building blocks you need to get this working, including a one-liner solution from @Olaf.

From your original post, you already know how to read and write to a file. So to modify my example you just need to replace the first and last lines of my example with the `Get-Content`/`Set-Content` commands.

[Next page](https://forums.powershell.org/t/integrate-do-until-looping-in-powershell/16558.md?page=2)
