0

All three of these methods display the results in similar manners. My question is why in METHOD 01, I can't use Write-Output {$_.FullName}, instead, I have to use a ForEach-Object?

I have to use METHOD 01 to run it against a function that I created.

Clear-Host

#METHOD 01
Get-ChildItem -Path 'D:\' -Directory -Depth 0 | 
ForEach-Object{$_.FullName} | 
Sort-Object

#METHOD 02 (Sticking into Variable)
$AA = Get-ChildItem -Path 'D:\' -Directory -Depth 0
Write-Output $AA.FullName | 
Sort-Object

#METHOD 03 (Sticking into Variable)
$AA = Get-ChildItem -Path 'D:\' -Directory -Depth 0
ForEach($oItem in $AA)
{
    $oItem.FullName
}
6
  • 1
    You can use Get-ChildItem | Write-Output -InputObject { $_.FullName }, which resolves the ambiguity between pipeline input and argument. Commented Aug 31, 2022 at 8:12
  • Zett42, I know I can use $Input.FullName to call it from a function. But how do I pass InputObject into my function? Get-ChildItem -Path 'D:\' -Directory -Depth 0 | MyFunction -InputObject {-FirstName $_.FullName -LastName $_.FullName } Commented Aug 31, 2022 at 8:50
  • Simplest way is a filter function: Filter MyFunction { $_ }. This would be a "do nothing" filter. For more complex stuff create an advanced function with a parameter that accepts pipeline input ([Parameter(ValueFromPipeline)] $InputObject). Commented Aug 31, 2022 at 9:15
  • Thanks and I agree on the advance function Zett42. I found this and it is what I am looking for. I am going to analyze it. Thank You and have a good night. Commented Aug 31, 2022 at 9:20
  • stackoverflow.com/questions/59434762/… Commented Aug 31, 2022 at 9:20

0

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.