Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a newline to command output in PowerShell?

I run the following code using PowerShell to get a list of add/remove programs from the registry:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `     | ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") } `     | Out-File addrem.txt 

I want the list to be separated by newlines per each program. I've tried:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `     | ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") `n } `     | out-file test.txt  Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `     | ForEach-Object {$_.GetValue("DisplayName") } `     | Write-Host -Separator `n  Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `     | ForEach-Object -Process { $_.GetValue("DisplayName") } `     | foreach($_) { echo $_ `n } 

But all result in weird formatting when output to the console, and with three square characters after each line when output to a file. I tried Format-List, Format-Table, and Format-Wide with no luck. Originally, I thought something like this would work:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `     | ForEach-Object -Process { "$_.GetValue("DisplayName") `n" } 

But that just gave me an error.

like image 770
mike Avatar asked Oct 28 '09 18:10

mike


People also ask

How do you add a new line in output?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do I skip to the next line in PowerShell?

In Powershell, SHIFT + ENTER takes you to the new line.

How do I run a multi line command in PowerShell?

To execute multiline command in one line in PowerShell, use a semicolon (;) after each command.

How do you use N in PowerShell?

New line (`n)The new line ( `n ) character inserts a line break immediately after the character. This example shows how to use the new line character to create line breaks in a Write-Host command.


2 Answers

Or, just set the output field separator (OFS) to double newlines, and then make sure you get a string when you send it to file:

$OFS = "`r`n`r`n" "$( gci -path hklm:\software\microsoft\windows\currentversion\uninstall |      ForEach-Object -Process { write-output $_.GetValue('DisplayName') } )" |   out-file addrem.txt 

Beware to use the ` and not the '. On my keyboard (US-English Qwerty layout) it's located left of the 1.
(Moved here from the comments - Thanks Koen Zomers)

like image 155
Jaykul Avatar answered Sep 17 '22 23:09

Jaykul


Give this a try:

PS> $nl = [Environment]::NewLine PS> gci hklm:\software\microsoft\windows\currentversion\uninstall |          ForEach { $_.GetValue("DisplayName") } | Where {$_} | Sort |         Foreach {"$_$nl"} | Out-File addrem.txt -Enc ascii 

It yields the following text in my addrem.txt file:

Adobe AIR  Adobe Flash Player 10 ActiveX  ... 

Note: on my system, GetValue("DisplayName") returns null for some entries, so I filter those out. BTW, you were close with this:

ForEach-Object -Process { "$_.GetValue("DisplayName") `n" } 

Except that within a string, if you need to access a property of a variable, that is, "evaluate an expression", then you need to use subexpression syntax like so:

Foreach-Object -Process { "$($_.GetValue('DisplayName'))`r`n" } 

Essentially within a double quoted string PowerShell will expand variables like $_, but it won't evaluate expressions unless you put the expression within a subexpression using this syntax:

$(`<Multiple statements can go in here`>). 
like image 38
Keith Hill Avatar answered Sep 17 '22 23:09

Keith Hill