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 ' , , <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 ' , , <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
Best Answer
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 .