Invoke-expression vs &


  1.    28 Nov 2007 #1
    Leo Tohill Guest

    Invoke-expression vs &


    What's the difference?

    For example,

    $t='notepad c:\temp\x.txt'
    &$t # this fails
    invoke-expression $t #this works

    More generally, I'm still puzzled about when the interpretation of strings
    occurs.

    tia

      My System SpecsSystem Spec

  2.    28 Nov 2007 #2
    Shay Levi Guest

    Re: Invoke-expression vs &


    Check out Keith's article, Effective PowerShell Item 10: Understanding PowerShell
    Parsing Modes
    http://keithhill.spaces.live.com/Blo...A97!6058.entry


    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com
    Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



    > What's the difference?
    >
    > For example,
    >
    > $t='notepad c:\temp\x.txt'
    > &$t # this fails
    > invoke-expression $t #this works
    > More generally, I'm still puzzled about when the interpretation of
    > strings occurs.
    >
    > tia
    >

      My System SpecsSystem Spec

  3.    28 Nov 2007 #3
    Keith Hill [MVP] Guest

    Re: Invoke-expression vs &


    "Shay Levi" <no@xxxxxx> wrote in message
    news:8766a944fdd88ca000612134012@xxxxxx

    > Check out Keith's article, Effective PowerShell Item 10: Understanding
    > PowerShell Parsing Modes
    > http://keithhill.spaces.live.com/Blo...A97!6058.entry
    Thanks. :-)

    >> $t='notepad c:\temp\x.txt'
    >> &$t # this fails
    In this case, the call operator (&) expects $t to be the *command* to
    execute and not an entire command line (with args, etc). IOW, while
    'notepad' is a valid command, 'notepad c:\temp\x.txt' isn't. This would
    work:

    $t = 'notepad'
    &$t c:\temp\x.txt

    >> invoke-expression $t #this works
    Invoke-Expression executes the whole string as if it were type in at the
    command line.

    --
    Keith

      My System SpecsSystem Spec

  4.    28 Nov 2007 #4
    Karl Prosser[MVP] Guest

    Re: Invoke-expression vs &


    yep & runs a command, invoke-expression first parses a string of text,
    and runs that, and thus is vulnerable to "powershell injection attacks".

    if you specify a preparsed scriptblock you can run that with the &, and
    unless your command is generated dynamically as a string, this is
    preferable over invoke-expression..


    $t={notepad c:\temp\x.txt}
    &$t # this fails



    or

    $mypath = '.....something that works';
    $t = {notepad $mypath}

    &$t
      My System SpecsSystem Spec

  5.    29 Nov 2007 #5
    Jon Guest

    Re: Invoke-expression vs &


    Probably simpler to use scriptblocks (curly brackets) with &, rather than
    strings, as Karl suggests. You don't have to be so concerned about what
    constitutes a command, and what constitues an argument, which also leads to
    simpler syntax eg with a string


    & 'notepad' c:\temp\x.txt #An executable
    & 'Get-Process' power* #A cmdlet
    function sayhello {"hi there!"} ; & "sayhello" #A function
    set-alias abc sayhello ; & "abc" #An alias

    whereas with a scriptblock, you can just use

    & {notepad c:\temp\x.txt } #Assuming the file exists
    & {Get-Process power*}

    etc

    --
    Jon

      My System SpecsSystem Spec

Invoke-expression vs &

Similar Threads
Thread Forum
invoke-expression problem
People I'm having a problem making a string from a number of variables (I've simplified the stuff below) then executing it, and have tried a...
PowerShell
Ambersand & Invoke-Expression
I need to use Invoke-Expression on a statement including ambersands & Example: Invoke-Expression "dir c:\ & if errorlevel 1 echo "PANIC!" My...
PowerShell
invoke-expression -computername ...
Does the Windows Firewall service have to be running in order to use WS-Management? I really like this remoting capability, however, all of our...
PowerShell
CTP: Invoke-Expression error
Hi I'm trying to run remote commands on a computer named "spare". Attached the error description. Both machines (source and destination) runs...
PowerShell
Invoke-expression help for logparser
Hi, I have been using logparser in windows command prompt. I thought to simplify my logparser scripts using powershell, But I have not been able...
PowerShell
log output from invoke-expression
Hi, How would one log all output including errors via "invoke-expression $cmd" ? thanks,
PowerShell
Issue: Invoke-Expression with $args in the expression
The following result of Invoke-Expression with $args in the expression seems unexpected: CODE: function fn1 { Write-Host fn1:, $($args +...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 08:42.
    .