1

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?

Jim
  • 111
  • 3
  • Unfortunately, in _Windows PowerShell_, `Get-ChildItem` output _situationally_ stringifies only to the mere file / folder _name_ rather than the _full path_. Fortunately, this is no longer a concern in _PowerShell (Core) 7+_. To be safe, use `$_.FullName`. See the linked duplicate for details. – mklement0 Dec 29 '22 at 19:13
  • [This article](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.3) on parsing might answer your question – eten Dec 29 '22 at 19:46

0 Answers0