• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

problem with invoke-expression

B

Ben Christian

#1
trying to open some documents, but some of the filenames have spaces in them,
and i'm not able to get invoke-expression to properly read it. I've tried
messing around with quotes, double quotes, parenthesis, etc, but can't seem
to get it to work...invoke expression stops reading the variable where the
first space in the path is...

here's the code snippet...


$files = @(get-childitem $searchpath | ? {$_.extension -match "pdf"})

foreach ($document in $files) {
write-host $document.fullname
invoke-expression $document.fullname
}

i've also just tried using $searchpath\$document instead of
$document.fullname. Neither work.

Thanks for your help!!
 

My Computer

K

Kiron

#2
Use Invoke-Item instead of Invoke-Expresion

foreach ($document in $files) {
write-host $document.fullname
invoke-item $document.fullname
}

--
Kiron
 

My Computer

K

Kiron

#3
You can also use the Call operator too:

foreach ($document in $files) {
write-host $document.fullname
& $document.fullname
}

--
Kiron
 

My Computer

K

Keith Hill [MVP]

#5
True but since the primary distinction between & and . (dot) is that dot executes the command in the current scope there isn't much benefit to using it when invoking external commands.

--
Keith
"Kiron" <Kiron@xxxxxx> wrote in message news:41032077-DEBE-4311-BDE6-0E5CB71301F6@xxxxxx
...or the Dot:

foreach ($document in $files) {
write-host $document.fullname
. $document.fullname
}

--
Kiron
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)