Home > Back-end >  Access to elements of $_
Access to elements of $_

Time:08-28

In order to create symlinks in PowerShell, you need to use the following command :

New-Item -Path C:\temp\myLink -Target C:\Users\{me}\Documents\target.txt -ItemType SymbolicLink

I would like to implement a more straightforward syntax as shown below :

"C:\temp\myLink", "C:\Users\{me}\Documents\target.txt" | Ln

Ln is a filter defined as such :

filter Ln {
     New-Item -Path $_[0] -Target  $_[1] -ItemType SymbolicLink
}

However, it doesn't work, and I would like to know how to access single elements of $_, for instance in our case shouldn't we have $_[0] == "C:\temp\myLink" ?

Thanks in advance for your help !

CodePudding user response:

If you want to use the syntax proposed in your question an easy workaround would be to change your enter image description here

If you want to send both elements together

enter image description here

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.2#comma-operator-

  • Related