0

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 = 
+
0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks this is getting at the objects but producing this: <br/> Test = @{Name=SQLWriter; PathName="C:\blah\sqlwriter.exe"}.PathName
Did you write this: "Test = $result.PathName"? You should write this instead: "Test = $($result.PathName)".
That got it, thanks. Any good site you recommend for picking up PS quickly? Is this site generally good for learning? powershell.com/cs/blogs/tips/archive/2009/06/08/… -- that page allowed me to get through the next part of my script
@Dird I mostly learned PowerShell by build-in documentation, MSDN, ILSpy to understand some internal working and practicing, so I can not recommend any good site for learning.
@Dird most people just pick it up "on the job" but I cannot recommend this book enough manning.com/payette2. If you manage to find it it's a really good in depth read
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.