Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

You cannot set a result of cmdlets to a property of object by Set-Variable

Context:

You can set a result of cmdlets to a variable by Set-Variable.

Problem:

You cannot set a result of cmdlets to a property of object by Set-Variable.

PS > $foo = New-Object PSObject | Select-Object Property
PS > Get-ChildItem | Set-Variable foo.Property
PS > $foo.Property
PS >

Reason:

It seems by design.

Solution:

Intead of using Set-Variable, simply assign value to property.

PS > $foo.Property = (Get-ChildItem)
PS > $foo.Property

    Directory: /home/satob

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
...