Powershell Out-File adds a new line to the top of the file - Out-File vs. Set-content

I have the following permissions:

# Find all .csproj files 
$csProjFiles = get-childitem ./ -include *.csproj -recurse 

# Remove the packages.config include from the csproj files.
$csProjFiles | foreach ($_) {(get-content $_) | 
             select-string -pattern '<None Include="packages.config" />' -notmatch | 
             Out-File $_ -force}

And it works fine. The line with package.config is not in the file after starting.

But after I started, a new line is added to this TOP file. (Not downstairs.)

I am confused by how this happens. What can I do to get rid of the extra new char line that is generated at the top of the file?

UPDATE:

I swapped another way:

$csProjFiles | foreach ($_) {$currentFile = $_; (get-content $_) | 
               Where-Object {$_ -notmatch '<None Include="packages.config" />'} | 
               Set-Content $currentFile -force}

It works fine and does not have an extra line at the top of the file. But I would not mind understanding why an extra line was added in the above example.

+4
source share
1

, , , , , /.

, :

  • Out-File >/>> UTF-16 LE, PowerShell Unicode -Encoding.
  • Set-Content "ANSI" (, ASCII- ), PowerShell Default.

    • , docs PSv5.1 , ASCII. [1]

:

  • Ad-hoc change. -Encoding Out-File Set-Content, .
    , >/>> ad-hoc, .

  • [PSv3 +] ( ): $PSDefaultParameterValues (. Get-Help about_Parameters_DefaultValues), :

    • Out-File >/>> PSv5.1 [2]. < > UTF-8, , :
      $PSDefaultParameterValues['Out-File:Encoding']='UTF8'

      • , PSv5.0 , > >> .
    • Set-Content, Add-Content:
      $PSDefaultParameterValues['Set-Content:Encoding'] = $PSDefaultParameterValues['Add-Content:Encoding'] ='UTF8'

    • / ; , $PSDefaultParameterValues['*:Encoding']='UTF8', cmdlets, -Encoding, , , -Encoding .

    • , , cmdlets, :
      $enc = 'UTF8; $PSDefaultParameterValues += @{ 'Out-*:Encoding'=$enc; 'Set-*:Encoding'=$enc; 'Add-*:Encoding'=$enc; 'Export-*:Encoding'=$enc }

    • Caveat: $PSDefaultParameterValues , .
      script/function , $PSDefaultParameterValues, -, ($PSDefaultParameterValues = @{}), ($PSDefaultParameterValues = $PSDefaultParameterValues.Clone())


[Microsoft.PowerShell.Commands.MatchInfo] Select-String:

  • , Out-File, ( , ).

  • .psobject.ToString() , Set-File, ( , , , -Path/-LiteralPath), .

, | Select-Object -ExpandProperty Line | ForEach-Object Line, , Out-File Set-Content ( ).


PS: LotPing : , , foreach ForEach-Object (, , foreach, ).

ForEach-Object cmdlet $_: ( -Process) script , $_ .

($_) foreach (ForEach-Object) : $null: $_, , , script - $null, (...) , $null, .


[1] , ASCII : '0x{0:x}' -f $('ä' | Set-Content t.txt; $b=[System.IO.File]::ReadAllBytes("$PWD\t.txt")[0]; ri t.txt; $b) 0xe4 en-US, Windows-1252 ä ( Unicode, ).
-Encoding ASCII, 0x3f, ?, , ASCII, , ASCII. .

[2] PetSerAl , , > >> Out-File [-Append], , Out-File >/>>; , $PSDefaultParameterValues Out-File >/>>.
Windows PowerShell v5.1 - , .

PetSerAl .

+4

Source: https://habr.com/ru/post/1670791/


All Articles