Get-Content pulling in []'s

Hello,

I’m doing a get-content on a file and on some blank lines (at least they look blank in the notepad file) are importing in with squares.

This is a copy/paste of them: here

Is there a way to do a get-content that will leave these pesky squares out?

May be an issue with the encoding. You can try outputting the content to a new file with ASCII

Get-content testfile.txt | Out-File -FilePath newfile.txt -Encoding ASCII

There are several way to use Get-Content and manipulate input on read as well as read and write.

Get-Content -Path D:\Temp -Raw

(Get-Content -Path D:\Temp) -replace ‘[]’

This will remove empty lines or lines with only whitespace characters (tabs/spaces).

[IO.File]::ReadAllText(“FileWithEmptyLines.txt”) -replace ‘\s+\r\n+’, “`r`n” `
| Out-File “c:\FileWithNoEmptyLines.txt”

This removes trailing whitespace and blank lines from file.txt

PS C:\Users> (gc file.txt) | Foreach {$.TrimEnd()} | where {$ -ne “”} | Set-Content file.txt

Or

(Get-Content c:\FileWithEmptyLines.txt) `
| ForEach { $_ -Replace “Old content”, " New content" } `
| Set-Content c:\FileWithEmptyLines.txt;

(get-content ‘c:\FileWithEmptyLines.txt’) -notmatch ‘^\s*$’ > c:\FileWithNoEmptyLines.txt

After looking at the .txt file it has a symbol in it that is showing up as the square in powershell console.

As shown here.

Is there a way to ignore this char? It pastes into the console as nothing

was able to find it by using the below method.

$lines = get-content c:\filepath\text.txt

The last line was showing up with the [] square symbol, so I did a

PS C:\Users\user> $lines[-1]
[] #note, not the actual symbol, it is just a square
#found it's ascii value by doing
PS C:\Users\user> [byte][char]$lines[-1]
12

then just parsed through the text file using

$lines = get-content c:\filepath\text.txt | ? {$_ -like "bk5013*"}).FullName | ? {$_ -notlike [char]12}

Thanks for the help!