I was trying to figure out how to create hardlinks in a destination folder to every file contained in a source folder. I finally succeeded by running this from the source folder:
gci | foreach-object {new-item -itemtype hardlink -path c:\destdir\$_ -value $_}
A hardlink named "1.txt" was created in c:\destdir to the file named "1.txt" in c:\sourcedir, and so on for every file in c:\sourcedir. So in -value $_
(source file in c:\sourcedir), the $_
resolves to the fileinfo object that will be hardlinked. But in -path c\destdir\$_
, the $_
resolved to "1.txt" - just the file name, not the whole pathname. How did Powershell know how to supply the right part of the filename from the fileinfo object that came from the pipeline?