No idea why it works (Get-childitem pipe to foreach-object)

Get-ChildItem -Path D:\Users | 
        $abc_exist = test-path -path "D:\Users\$_\Favorites\AB C D.url"       
        if ($abc_exist){
               -- some actions

Dear all,

I can make the script worked by try-and-error approach.

At first, I used

        $abc_exist = test-path -path "D:\Users\$\Favorites\AB C D.url"       

But it didn’t work. I use “Write-out” and find out that the path will look like “D:\Users\\Favorities\AB C D.url” this in the output.
Then I just leave $_ in the code and now it works. As I thought, after get-childitem pipe, the whole object will be moved to the next action “ForEach_Object”. I thought I need to specify the property “Name” instead of leaving the whole object in the file path.

Did I have the wrong concept about this? Many thanks for the newbie question.

If you want to specify a particular property of an object inside a string you need to use a subexpression … like this:

"D:\Users\$($_.Name)\Favorites\AB C D.url"

I don’t have a technical explanation for this. But I’d try a wild guess and say PowerShell is made for admins not developers. So there probably is a mechanism to choose a default property best fitting to the target object to make the live easier for administrators. :wink:

It is clear now!! Many thanks for the explanations.