1

I want to save an output to a variable, but it saves more characters than I want to save.

I use: $InstallLocation1 = Get-WmiObject -Class Win32_Product -Filter 'Name like "%FAS%"' | Select InstallLocation

But then my variable contains: @{InstallLocation=C:\Program Files\inray\FAS\}

But the content I need is: C:\Program Files\inray\FAS\

1
  • - In short: Select-Object (select) by default returns a [pscustomobject] instance that has the requested properties - even when you're only asking for a single property. To get only that property's value, use the -ExpandProperty parameter instead of the (possibly positionally implied) -Property parameter. See the linked duplicate for details and alternatives, notably the ability to simply use (...).SomeProperty Commented Oct 11, 2024 at 17:20

1 Answer 1

3

... |Select-Object <propertyName>,... will create a new object and copy the properties from the input object to the new object - so you end up with a new object that has exactly 1 property named InstallLocation.

To grab only the value of a single property, use ForEach-Object -MemberName instead:

$InstallLocation1 = Get-WmiObject -Class Win32_Product -Filter 'Name like "%FAS%"' |ForEach-Object -MemberName InstallLocation

Alternatively you can (ab)use Select-Object's -ExpandProperty parameter to achieve the same outcome:

$InstallLocation1 = Get-WmiObject -Class Win32_Product -Filter 'Name like "%FAS%"' |Select-Object -ExpandProperty InstallLocation
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.