2

I have the following:

function createFolder($folderName, $curPath)
{
    $dest = $curPath + $folderName
    write-host "Path is : " + $dest
    # code to mess around with files etc
}

When I run this it gives me the following output:

Path is : + Test_Folder C:\Users\Me

Is there something I'm missing with the + operator, or a join/concat method that is meant for this kind of function? What is the correct way to create/concat/manipulate paths in PowerShell (I've just started using this to automate some cleanup tasks on my desktop).

EDIT: In case it makes a difference, this is what I see when I run the version command:

PS C:\Users\Me> version
BladeLogic Network Shell 8.2.01.273 (Release) [May 12 2012 21:56:02]
Copyright (C) 1996-2012 BladeLogic Inc.

Also, I'm on a work computer with no administrative privileges.

I tried:

$currentPath = "C:\Users\n0223270\Downloads"
$test = "test"
createFolder($test, $currentPath)

function createFolder($folderName, $curPath)
{
    $dest = join-path -path $curPath -childpath $folderName
    Write-Host $dest   
}

This was the following error:

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At line:4 char:28
+     $dest = join-path -path <<<<  $curPath -childpath $folderName
+ CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand
5
  • 1
    Try using the Join-Path cmdlet instead of string concatenation. Commented Jul 6, 2015 at 14:35
  • What's $curPath? What's $folderName? + isn't string concat, you can just put variables in a string or use -f to format variables into a string (which is usually better). Commented Jul 6, 2015 at 14:37
  • $curPath is the current working directory, so in the above example: $curPath = "C:\Users\Me" and $folderName is the name of a folder to be created if it doesn't exist and used as the destination for a copy and paste job later on in the function, in the above: $folderName = "Test_Folder" Commented Jul 6, 2015 at 14:41
  • @EBGreen I tried the Join-Path cmdlet, updated question with the results Commented Jul 6, 2015 at 14:44
  • You're using parens and commas to call a powerShell function. Try createFolder $test $currentPath Commented Jul 6, 2015 at 14:48

1 Answer 1

5

createFolder($test, $currentPath)

This isn't how you call a Powershell function. This is passing the first parameter as an array of two strings. The second parameter would be null because it's not specified and there's no default.

Try:

createFolder $test $currentPath
Sign up to request clarification or add additional context in comments.

2 Comments

Ok that worked just fine, thanks. Still trying to migrate from a Java background to powershell scripting
@jbailie1991 It's definitely non-obvious if you haven't encountered it before because the language is very C-like. IMX, the more you use it the more you'll use named parameters with the param() construct instead of the C-like function definition. See the Get-Help about_Functions* articles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.