Reputation: 33078
I'm using the following Powershell command to copy one user's Mercurial.ini file into another user's profile:
> copy $env:USERPROFILE\..\benm\Mercurial.ini $env:USERPROFILE\..\alex\Mercurial.ini
That works well. However, I'd like to write it in such as way so that I can do something like the following, specifying the users up front and piping them in to the copy command:
> "benm","alex" | copy "$env:UserProfile\..\$_[0]\Mercurial.ini" "$env:UserProfile\..\$_[1]\Mercurial.ini"
However, the above doesn't work. Is there an elegant way to achieve what I'm trying to do?
Upvotes: 1
Views: 3515
Reputation: 68243
Something like this?
,("benm","alex") |
foreach {copy "$env:UserProfile\..\$($_[0])\Mercurial.ini" "$env:UserProfile\..\$($_[1])\Mercurial.ini"}
The ,("benm","alex") syntax makes the input from the pipeline a 2D array
PS C:\> $x = ,("benm","alex")
PS C:\> $x[0]
benm
alex
PS C:\> $x[0][0]
benm
PS C:\> $x[0][1]
alex
The Powershell pipeline will automatically "unroll" arrays and collections into a stream of objects, but only one level deep for nested arrays. By making it a 2D array, it "unrolls" the first level, and passes the nested array through the pipelile intact.
Upvotes: 3