Partying with Join-Path

Steve Lee

Steve

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 th
e path delimiters. The Path parameter is required, although the paramet
er name (-path) is optional.

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

-childPath
Specifies the elements to append to the value of Path. Wildcards are pe
rmitted. The ChildPath parameter is required, although the parameter na
me (-ChildPath) is optional.

Required? true
Position? 2
Default value N/A – The ChildPath must be specified
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? True
Let’s get crazy!
Notice that PATH accepts an array of STRINGS. This is what that allows you to do:
PS> Join-Path C:\hello world
C:\hello\world
PS> Join-Path C:\hello,d:\goodbye,e:\hola,f:\adios world
C:\hello\world
d:\goodbye\world
e:\hola\world
f:\adios\world
Next, notice that PATH can be pipelined BYVALUE. That allows you to do:
PS> Get-Content t1.txt
c:\hello
d:\Goodbye
e:\hola
f:\adios
PS> Get-Content t1.txt | Join-Path -ChildPath world
c:\hello\world
d:\Goodbye\world
e:\hola\world
f:\adios\world
Next, notice that ChildPath can be pipelined BYPROPERTYNAME. That allows you to do:
PS> Get-Content t.csv
Childpath
World
Jeffrey
Microsoft

PS> Import-Csv t.csv |Join-Path c:\hello
c:\hello\World
c:\hello\Jeffrey
c:\hello\Microsoft
c:\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-Bye
c:\hello\World
d:\Good-Bye\World
c:\hello\Jeffrey
d:\Good-Bye\Jeffrey
c:\hello\Microsoft
d:\Good-Bye\Microsoft
c:\hello\
d:\Good-Bye\
Lastly, notice that both PATH and CHILDPATH can be pipelined BYPROPERTYNAME. That allows you to do:
PS> Get-Content t2.csv
Path,Childpath
c:\Hello,World
d:\Good-Bye,Jeffrey
e:\PowerShell-Rocks,Microsoft
PS> Import-Csv t2.csv |Join-Path
c:\Hello\World
d:\Good-Bye\Jeffrey
e:\PowerShell-Rocks\Microsoft
So 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 Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Steve Lee
Steve Lee

Principal Software Engineer Manager, PowerShell

Follow Steve   

No Comments.