Partying with Join-Path

Steve Lee

Did you realize that you can Join-Path can work on multiple items?PS> Get-Help Join-Path -parameter *path

-path Specifies the main path (or paths) to which the child-path is appended.Wildcards are permitted.

The value of Path determines which provider joins the paths and adds the path delimiters. The Path parameter is required, although the parameter name (-path) is optional.

Required? truePosition? 1Default value N/A – The path must be specifiedAccept pipeline input? true (ByValue, ByPropertyName)Accept wildcard characters? true

-childPath Specifies the elements to append to the value of Path. Wildcards are permitted. The ChildPath parameter is required, although the parameter name (-ChildPath) is optional.

Required? truePosition? 2Default value N/A – The ChildPath must be specifiedAccept pipeline input? true (ByPropertyName)Accept wildcard characters? TrueLet’s get crazy!Notice that PATH accepts an array of STRINGS. This is what that allows you to do:PS> Join-Path C:\hello worldC:\hello\worldPS> Join-Path C:\hello,d:\goodbye,e:\hola,f:\adios worldC:\hello\worldd:\goodbye\worlde:\hola\worldf:\adios\worldNext, notice that PATH can be pipelined BYVALUE. That allows you to do:PS> Get-Content t1.txtc:\hellod:\Goodbyee:\holaf:\adiosPS> Get-Content t1.txt | Join-Path -ChildPath worldc:\hello\worldd:\Goodbye\worlde:\hola\worldf:\adios\worldNext, notice that ChildPath can be pipelined BYPROPERTYNAME. That allows you to do:PS> Get-Content t.csvChildpathWorldJeffreyMicrosoft

PS> Import-Csv t.csv |Join-Path c:\helloc:\hello\Worldc:\hello\Jeffreyc:\hello\Microsoftc:\hello\Next, remember that Path accepts an ARRAY of strings. This allows you to do:PS> Import-Csv t.csv |Join-Path c:\hello,d:\Good-Byec:\hello\Worldd:\Good-Bye\Worldc:\hello\Jeffreyd:\Good-Bye\Jeffreyc:\hello\Microsoftd:\Good-Bye\Microsoftc:\hello\d:\Good-Bye\Lastly, notice that both PATH and CHILDPATH can be pipelined BYPROPERTYNAME. That allows you to do:PS> Get-Content t2.csvPath,Childpathc:\Hello,Worldd:\Good-Bye,Jeffreye:\PowerShell-Rocks,MicrosoftPS> Import-Csv t2.csv |Join-Pathc:\Hello\Worldd:\Good-Bye\Jeffreye:\PowerShell-Rocks\MicrosoftSo now what you’ll want to do is to a bunch of Get-Help –FULL and example the parameters and look for those that accept arrays of elements and those that accept pipeline input – then experiment.Enjoy!Jeffrey Snover [MSFT]Windows Management Partner ArchitectVisit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx