0

New to powershell and basically have plagiarize the following script to scan through a list of html files to remove unnecessary commas. I only get an error when running this on the server. Could use some suggestions or advice on how to fix. Here is the script.

Get-ChildItem C:\temp\emailsigs -Filter *.htm | ForEach-Object { $oldContent = Get-Content -Raw $_.FullName $newContent = $oldContent -replace ' , &nbsp; , &nbsp; <br />' if ($newContent.Length -lt $oldContent.Length) { # was a replacement performed? Set-Content $_.FullName -NoNewline -Value $newContent } } 

When I test this on my PC, it works. When I run this on the server hosting the html files, I adjust the file location:

Get-ChildItem C:\EmailSignatures -Filter *.htm | ForEach-Object { $oldContent = Get-Content -Raw $_.FullName $newContent = $oldContent -replace ' , &nbsp; , &nbsp; <br />' if ($newContent.Length -lt $oldContent.Length) { # was a replacement performed? Set-Content $_.FullName -NoNewline -Value $newContent } } 

But I get the following error:

PS C:\_dev> C:\_dev\EsigCleanup_HTMV3.ps1 Set-Content : A parameter cannot be found that matches parameter name 'NoNewline'. At C:\_dev\EsigCleanup_HTMV3.ps1:6 char:31 + Set-Content $_.FullName -NoNewline -Value $newContent + ~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-Content], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand 
1
  • It's probably completely unrelated to your problem, but if your file path contains [ or ] characters, you will get this message. Swithching to -LiteralPath made it go away (yes, really...) Commented Mar 29, 2022 at 20:23

1 Answer 1

3

Are the server and your PC running the same version of PowerShell (check $PSversionTable)? NoNewLine was added in PowerShell 5.0.

Edit: there is an extremely detailed answer on StackOverflow that addresses options for lower versions of PowerShell - https://stackoverflow.com/questions/35279848/prevent-trailing-newline-in-powershell-out-file-command .

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.