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.
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.
In Powershell, SHIFT + ENTER takes you to the new line.
To execute multiline command in one line in PowerShell, use a semicolon (;) after each command.
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.
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)
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`>).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With