How to add a new line to output a command in PowerShell?

I run the following code using PowerShell to get a list of programs to add / remove 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 for each program. I 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 lead to strange formatting when outputting to the console and with three square characters after each line when outputting to a file. I tried Format-List , Format-Table and Format-Wide with no luck. Initially, 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 a mistake.

+55
powershell newline
Oct 28 '09 at 18:45
source share
5 answers

Or simply set the output field separator (OFS) to double newline characters, and then make sure you get the string when sending it to a 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 of using ' rather than ' . On my keyboard (the Anglo-American Qwerty layout) it is located to the left of 1 .
(Moved here from comments - thanks to Cohen Somers)

+74
Oct 29 '09 at 5:39
source share

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 displays the following text in the addrem.txt file:

 Adobe AIR Adobe Flash Player 10 ActiveX ... 

Note. On my system, GetValue ("DisplayName") returns null for some records, so I filter them out. By the way, you were close with this:

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

Except that inside the string, if you need to access the property of a variable, that is, "evaluate the expression", then you need to use the subexpression syntax, for example:

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

Essentially, in a double-quoted string, PowerShell will expand variables like $_ , but will not evaluate expressions unless you add an expression to a subexpression using this syntax:

 $(`<Multiple statements can go in here`>). 
+69
Oct 28 '09 at 18:53
source share

Ultimately, what you are trying to do with EXTRA spaces in between is a bit confusing :)

I think you really want to use Get-ItemProperty . You will get errors when values ​​are missing, but you can turn them off with -ErrorAction 0 or just leave them as reminders. Because the registry provider returns additional properties, you'll want to stick with Select-Object , which uses the same properties as Get-Properties.

Then, if you want each property to be in a row with an empty row between them, use Format-List (otherwise use Format-Table to get one row each).

 gci -path hklm:\software\microsoft\windows\currentversion\uninstall | gp -Name DisplayName, InstallDate | select DisplayName, InstallDate | fl | out-file addrem.txt 
+6
Oct 29 '09 at 13:24
source share

I think you had the right idea with your last example. You encountered an error because you tried to put quotation marks inside an already specified string. This will fix this:

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

Edit: the Keith $ () operator actually creates the best syntax (I always forget about it). You can also escape quotes inside quotes like this:

 gci -path hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object -Process { write-output "$($_.GetValue(`"DisplayName`"))`n" } 
+5
Oct 28 '09 at 21:16
source share

The option that I usually use, mainly because it is simple and I don’t need to think, uses Write-Output , as shown below, Write-Output will put the EOL marker on the line for you, and you can simply output the completed line.

 Write-Output $stringThatNeedsEOLMarker | Out-File -FilePath PathToFile -Append 

Alternatively, you can also simply build the entire line using Write-Output, and then click the finished line in the Out-File .

+4
Feb 21 '13 at 18:00
source share



All Articles