title: New-Item changes function return value
body: <p>I want to write a function that creates a folder (if it doesn't exist) with specified name inside specified folder.
  It's turned out that depending on calling <code>New-Item</code> function return different values. And I can't figure out how it's related to <code>New-Item</code> actually.</p>
  
  <pre><code>$folderPath = "C:\tmp"
  
  function CreateFolder([string] $name, [string] $parentFolder) 
  {
    $path = "$parentFolder\$name"
  
    if(!(Test-Path $path))
    {
      New-Item -path $parentFolder -name $name -itemtype Directory
    }
  
    return $path
  }
  
  $FOLDER_NAME = "folder1"
  
  $destination = CreateFolder $FOLDER_NAME $folderPath
  
  echo $destination.GetType()
  </code></pre>
  
  <p>If <strong>folder1 doesn't exist</strong> it'll return:</p>
  
  <pre><code>IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     Object[]                                 System.Array
  </code></pre>
  
  <p>Otherwise:</p>
  
  <pre><code>IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     String                                   System.Object
  </code></pre>
  
  <p>It wouldn't be an issue unless <code>Move-Item</code> supported <code>Object[]</code> as <code>-destination</code> parameter.</p>
  
  <p><code>echo $destination</code> returns:</p>
  
  <pre><code>PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\tmp\folder1
  PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\tmp
  PSChildName       : folder1
  PSDrive           : C
  PSProvider        : Microsoft.PowerShell.Core\FileSystem
  PSIsContainer     : True
  Name              : folder1
  Parent            : tmp
  Exists            : True
  Root              : C:\
  FullName          : C:\tmp\folder1
  Extension         : 
  CreationTime      : 13.08.2015 10:53:11
  CreationTimeUtc   : 13.08.2015 7:53:11
  LastAccessTime    : 13.08.2015 10:53:11
  LastAccessTimeUtc : 13.08.2015 7:53:11
  LastWriteTime     : 13.08.2015 10:53:11
  LastWriteTimeUtc  : 13.08.2015 7:53:11
  Attributes        : Directory, NotContentIndexed
  BaseName          : folder1
  Target            : 
  LinkType          : 
  Mode              : d-----
  </code></pre>
  
  <p>The only solution I found is not use the function:</p>
  
  <pre><code>$folderPath = "C:\tmp"
  
  $FOLDER_NAME = "folder1"
  
  $destination = "$folderPath\$FOLDER_NAME"
  
  if(!(Test-Path $destination))
  {
    New-Item -path $folderPath -name $FOLDER_NAME -itemtype Directory
  }
  
  Move-Item -path "C:\tmp\file1" -destination $destination
  </code></pre>
  
  <p>If folder1 doesn't exist:</p>
  
  <pre><code>    Каталог: C:\tmp
  
  
  Mode                LastWriteTime         Length Name                                                                                                                    
  ----                -------------         ------ ----                                                                                                                    
  d-----       13.08.2015     11:06                folder1                                                                                                                 
  
  MemberType                 : TypeInfo
  DeclaringType              : 
  DeclaringMethod            : 
  ReflectedType              : 
  StructLayoutAttribute      : System.Runtime.InteropServices.StructLayoutAttribute
  GUID                       : 296afbff-1b0b-3ff5-9d6c-4e7e599f8b57
  Module                     : CommonLanguageRuntimeLibrary
  Assembly                   : mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  TypeHandle                 : System.RuntimeTypeHandle
  FullName                   : System.String
  Namespace                  : System
  AssemblyQualifiedName      : System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  BaseType                   : System.Object
  TypeInitializer            : 
  IsNested                   : False
  Attributes                 : AutoLayout, AnsiClass, Class, Public, Sealed, Serializable, BeforeFieldInit
  GenericParameterAttributes : 
  IsVisible                  : True
  IsNotPublic                : False
  IsPublic                   : True
  IsNestedPublic             : False
  IsNestedPrivate            : False
  IsNestedFamily             : False
  IsNestedAssembly           : False
  IsNestedFamANDAssem        : False
  IsNestedFamORAssem         : False
  IsAutoLayout               : True
  IsLayoutSequential         : False
  IsExplicitLayout           : False
  IsClass                    : True
  IsInterface                : False
  IsValueType                : False
  IsAbstract                 : False
  IsSealed                   : True
  IsEnum                     : False
  IsSpecialName              : False
  IsImport                   : False
  IsSerializable             : True
  IsAnsiClass                : True
  IsUnicodeClass             : False
  IsAutoClass                : False
  IsArray                    : False
  IsGenericType              : False
  IsGenericTypeDefinition    : False
  IsConstructedGenericType   : False
  IsGenericParameter         : False
  GenericParameterPosition   : 
  ContainsGenericParameters  : False
  IsByRef                    : False
  IsPointer                  : False
  IsPrimitive                : False
  IsCOMObject                : False
  HasElementType             : False
  IsContextful               : False
  IsMarshalByRef             : False
  GenericTypeArguments       : {}
  IsSecurityCritical         : False
  IsSecuritySafeCritical     : False
  IsSecurityTransparent      : True
  UnderlyingSystemType       : System.String
  Name                       : String
  CustomAttributes           : {[System.SerializableAttribute()], [System.Reflection.DefaultMemberAttribute("Chars")], [System.Runtime.InteropServices.ComVisibleAttribute(
                           (Boolean)True)], [__DynamicallyInvokableAttribute()]}
  MetadataToken              : 33554536
  </code></pre>
  
  <p>Otherwise:</p>
  
  <pre><code>IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     String                                   System.Object
  </code></pre>
  
  <hr>
  
  <p><strong>Working example</strong> after Ansgar's recommendations:</p>
  
  <pre><code>$folderPath = "C:\tmp"
  
  function GetDestination {
    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$true)]
      [string]$Name,
  
      [Parameter(Mandatory=$true)]
      [string]$ParentFolder
    )
  
    $path = Join-Path $ParentFolder $Name
  
    if(Test-Path -LiteralPath $path) {
      Get-Item -LiteralPath $path
    } else {
      New-Item -Path $ParentFolder -Name $Name -ItemType Directory
    }
  }
  
  $FOLDER_NAME = "folder1"
  
  $destination = GetDestination $FOLDER_NAME $folderPath
  
  Move-Item -LiteralPath "C:\tmp\file1" -Destination $destination
  </code></pre>