Dird
Dird

Reputation: 325

Powershell object attribute not showing

Any idea why the $result.PathName doesn't work?

$results = Get-WMIObject -query "select * from Win32_Service where Name LIKE '%sql%'" | Select Name, PathName | Format-List
ForEach ($result in $results) {
    Write-Output "Test = " + $result.PathName
}        
Write-Output "done"

Expected output:

Test = C:\blah\blah.sqlserver.exe
Test = C:\blah\blah.sqlserver.exe

Actual output

Test =
+
Test = 
+

Upvotes: 0

Views: 245

Answers (1)

user4003407
user4003407

Reputation: 22102

Format-* cmdlets produce formatting instructions as output. This instructions not useful for anything except displaying result to user or for Out-* cmdlets. Remove Format-List from your code. And put expression after Write-Output in parentheses: Write-Output ("Test = " + $result.PathName), to it be interpreted as single expression but not three different arguments to Write-Output. Or you can remove Write-Output entirely as it implied by default.

Upvotes: 3

Related Questions