how to build URI string in Invoke-WebRequest command

This syntax works:

$RootPath = 'http://localhost:5000'
$Response = Invoke-WebRequest -URI $RootPath

But if I change the second line trying to build a longer URI in the same line, like this:

$Response = Invoke-WebRequest -URI $RootPath + '/Foo'

It gets the error:

Invoke-WebRequest : A positional parameter cannot be found that accepts argument '+'.

The plus operator works in other contexts to build strings. What is the correct syntax to do it in this case? (I’ve done some online searching but can’t find it.) Thanks for any pointer.

A few ways to do this:



$fullPath= ($URL + $URI)

invoke-webrequest $fullPath


That’s essentially the same as what I did. My question was how to build the parameter within the line

Invoke-WebRequest -URI $RootPath + '/Foo'

which is sometimes more compact and convenient.

Parenthesizing like this works. Solved.

Invoke-WebRequest -URI ($RootPath + '/Foo')

There’s another way that’s a little shorter. :slight_smile:

Invoke-WebRequest -Uri "$RootPath/Foo"

Alternately you can have the System.Uri class handle the join and take care of the occasional pesky extra slash, etc.

Invoke-WebRequest -Uri ([uri]::new($RootPath,'/Foo'))

I’m a big fan of the c# style string builder.Here is your example, and I’ve made a few other examples on string building as well.
PS> “{0}{1}” -f “”,"/relativepath"

Other examples: