Windows Vista Forums

problem with invoke-expression

  1. #1


    Ben Christian Guest

    problem with invoke-expression

    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 System SpecsSystem Spec

  2. #2


    Kiron Guest

    Re: problem with invoke-expression

    Use Invoke-Item instead of Invoke-Expresion

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

    --
    Kiron

      My System SpecsSystem Spec

  3. #3


    Kiron Guest

    Re: problem with invoke-expression

    You can also use the Call operator too:

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

    --
    Kiron

      My System SpecsSystem Spec

  4. #4


    Kiron Guest

    Re: problem with invoke-expression

    ...or the Dot:

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

    --
    Kiron

      My System SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: problem with invoke-expression

    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 System SpecsSystem Spec


problem with invoke-expression
Similar Threads
Thread Forum
invoke-expression problem PowerShell
Invoke-Expression issue PowerShell
Invoke-expression vs & PowerShell
invoke-expression not appending PowerShell
Issue: Invoke-Expression with $args in the expression PowerShell