Call properties / methods in a Piped Object

I am trying to understand how pipe | object and call properties or methods to do this.

 Ex: $a = Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\ $a.GetSomething() //calls the method (Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\).GetSomething() //calls the method 

Can I skip the output of Get-Item and invoke properties/methods on it?

 Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\ | call GetSomething() 
+8
source share
4 answers

The answer is no . You cannot call a method like this using Pipeline. But you can surround your Get-Item call in parentheses and call it:

 (Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\).GetSomething() 

If you do not want this, you can use the Select-Object :

 Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\ | select { $_.GetSomething() } 
+2
source

It is impossible without writing something to do it like this. Something would be rather confusing.

Like this.

 filter Invoke-Method { param( [String]$Method, [Object[]]$ArgumentList ) $_.GetType().InvokeMember( $Method.Trim(), ([System.Reflection.BindingFlags]'InvokeMethod'), $null, $_, $ArgumentList ) } "qwerty" | Invoke-Method Replace 'q', 'z' 

Properties are simpler in that there is already a command for this:

 (...).GetSomething() | Select-Object Property1, Property2 
+1
source

In my opinion, a better method would be to use, for example:

Get-Item Registry::HKLM | % { $_ } Get-Item Registry::HKLM | % { $_ } Get-Item Registry::HKLM | % { $_ } Get-Item Registry::HKLM | % { $_ } :

where % is ForEach-Object (which can be used even if you only have 1), and $_ is every object.

0
source

Ansgar Wichers provided a key pointer in a commentary on Martin Brandle's answer:

The canonical way would be ForEach-Object . This can be written quite briefly using the alias % :
... | % { $_.GetSomething() }

In PowerShell version 3 or higher, you can make the call even more concise with the operation operator , which eliminates the need to enclose the call in {... } , explicitly reference $_ and use parentheses ( (...) ):

 ... | % GetSomething # short for: ... | ForEach-Object GetSomething 

Note that if a method accepts arguments, they must be represented as an array ( , -separated), but without enclosing them in (...) , since the arguments are syntactically passed in argument mode , which also simplifies quoting a simple string; optional values ​​- see examples below.

Examples :

Method call without arguments:

 # Perform the equivalent of: # (Get-Item Registry::HKLM\SOFTWARE\Classes\txtfile).GetValueNames() # That is, get the names of the values defined on registry key # HKEY_LOCAL_MACHINE\SOFTWARE\Classes\txtfile PS> Get-Item Registry::HKLM\SOFTWARE\Classes\txtfile | % GetValueNames EditFlags FriendlyTypeName 

A method call with one argument:

 # Perform the equivalent of (Get-Date).ToString('u'); ie, # get a date universally sortable string representation. PS> Get-Date | % ToString u # "u" may be quoted, but doesn't need to be. 2019-04-19 08:22:22Z 

Calling a method with multiple arguments:

 # Perform the equivalent of 'foo'.Replace('f', 'F'); ie, # replace all lowercase Fs with uppercase ones. PS> 'foo' | % Replace f F Foo 
0
source

All Articles