Search code examples
stringpowershellpathconcatenation

How to concat path and file name for creation and removal


I'm trying the following.

$woohoo = "c:\temp\shabang"
New-Item -ItemType File $woohoo

This creates a file at desired location. However, I'd like to split the $woo and $hoo and go something like this.

$woo = "c:\temp"
$hoo = "shabang"
New-Item -ItemType File ($woo + $hoo)

This doesn't work and I need a hint on how to make it fly. This suggestion nor this on worked out when applied to my situation. Apparently - given path format is unsupported.

Suggestions?

Edit

This is what it looks like: enter image description here

Edit 2

$woo = "c:\temp";
$hoo= "shabang";
New-Item -ItemType File (Join-Path $woo $hoo)

Solution

  • Doing $woo + $hoo does not return the proper filepath:

    PS > $woo = "c:\temp"
    PS > $hoo = "shabang"
    PS > $woo + $hoo
    c:\tempshabang
    PS >
    

    Instead, you should use the Join-Path cmdlet to concatenate $woo and $hoo:

    New-Item -ItemType File (Join-Path $woo $hoo)
    

    See a demonstration below:

    PS > $woo = "c:\temp"
    PS > $hoo = "shabang"
    PS > Join-Path $woo $hoo
    c:\temp\shabang
    PS >