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`>).
Keith Hill Oct 28 '09 at 18:53 2009-10-28 18:53
source share