Partying with Join-Path

Partying with Join-Path

  • Comments 2

Did you realize that you can Join-Path can work on multiple items?

PS> Get-Help Join-Path -parameter *path

-path <string[]>
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 <string>
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

Leave a Comment
  • Please add 1 and 7 and type the answer here:
  • Post
  • 1. You can submit a new bug or suggestion on Mcrosoft connect [link] 2. Can't remember :( 3. Mabe this

  • Well, it does everything but what I want it to do.  I would like one call to join-path to put multiple parts together into 1.  For example, I hoped that

    $a = "one", "two"

    join-path -path "c:\temp" $a

    would give me

    c:\temp\one\two

    alas, no joy.

Page 1 of 1 (2 items)