Iwan1993
Iwan1993

Reputation: 1779

Powershell Join Path returns array

I am trying to create folders at the start of my script and afterwords create files in those. To create paths i use md -Force $path, i also used just to test similar methods

if(![System.IO.Directory]::Exists($path)){
    [System.IO.Directory]::CreateDirectory($path)
}

After creating the path i like to build the filename + path of the file i want to write to, i went 2 ways:

Join-Path $path "myfile.txt"

which leads to the result that i get an string array as result, with two records, both building the same path $path\myfile.txt Also using

[System.IO.Path]::Combine($path, "myfile.txt")

Returns the same string array, with two records (even though the combine method has only a definition for returning a string)

This behavior only occurs if i create the folders before, so i would like to know what causes these results and how can i avoid them.

My Setup is Powershell 3 (customer "wish") with Win7.

Upvotes: 1

Views: 759

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200353

Your variable $path contains an array.

PS C:\> $path = 'C:\Temp'
PS C:\> $res = Join-Path $path 'file.txt'
PS C:\> $res.GetType().FullName
System.String
PS C:\> $res
C:\Temp\file.txt
PS C:\> $path = 'C:\Temp', 'C:\Windows'
PS C:\> $res = Join-Path $path 'file.txt'
PS C:\> $res.GetType().FullName
System.Object[]
PS C:\> $res
C:\Temp\file.txt
C:\Windows\file.txt

Upvotes: 3

Related Questions