Powershell: Save files keeping the same name, without rename it + Out-File -FilePath ()

just to understand the saving option on Powershell. The code below mix words from a particular file, into one folder, it save that file with a different name.

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

Now, I modify a little bit the code. Because I want to select all text files, from the same folder, but save them with the same name. So I want to modify each files, but keeping the original name on save.

I believe I made something wrong. Can you help me, please?

((Get-Content -Path 'C:\Folder1' -Filter '*.txt' -Raw -Encoding UTF8) -split "\s+" | 
    Sort-Object {Get-Random} ) -join ' ' |
        Out-File -FilePath "$newfilename" -force

I get this error:

Get-Content : Could not find a part of the path 'C:\Folder1\'.
At line:1 char:3
+ ((Get-Content -Path 'C:\Folder1\' -Filter '*.txt' -Raw -Encodi ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Folder1\:String) [Get-Content], DirectoryNotFoundException
    + FullyQualifiedErrorId : GetContentReaderDirectoryNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand

Out-File : Cannot bind argument to parameter 'FilePath' because it is an empty string.
At line:3 char:28
+         Out-File -FilePath "$newfilename" -force
+                            ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.OutF
   ileCommand

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.

Thanks

yes, I need quickly the answer.

The sourse of an answer HERE


$Fllist=get-childitem "c:\Folder4\docx\4\" | ?{$_.name -ilike "*.txt"}

foreach($File in $Fllist) {
    ((Get-Content -Path "c:\Folder4\docx\4\$($File.name)" -Raw -Encoding UTF8) -split "\s+" |
      Sort-Object {Get-Random} ) -join ' ' |
      Out-File -FilePath "c:\Folder4\docx\4\$($File.name)"
}

It only takes about 10 seconds. And it even might speed up to get an answer. :wink:

I will know next time. Thank you