How to use previous command output as a parameter in Powershell?

Viewed 138

I need to write a Powershell one-liner to do a complex task. I need it to be strictly one line because I want to run it in a Go and Python script. The task requires taking the output of the first command and use it as a parameter in the second command.

I thought it was a simple task but I am struggling with it quite a bit. For example, the below command does not work:

$obj = Get-Item . | Get-Item $obj.Root | Format-List *
Get-Item : The input object cannot be bound to any parameters for the command either because the command does not take
pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:21
+ $obj = Get-Item . | Get-Item $obj.Root | Format-List *
+                     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Users\fhcat:PSObject) [Get-Item], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetItemCommand

What am I doing wrong?

1 Answers

Strictly one line sounds like a personal preference rather than a technical requirement.

However, you can end a statement with a semicolon or process the input via ForEach-Object

$obj = Get-Item .; Get-Item $obj.Root | Format-List *

or

Get-Item . | ForEach-Object {Get-Item $_.Root} | Format-List *
Related