Alternatives to invoke-expression

powershell invoke-expression
powershell start-process vs invoke-expression
invoke-expression wait
invoke-expression credential
powershell invoke-expression output
alternative to invoke-command
invoke-expression async
invoke-expression erroraction

I have this function:

function traced()
    write-host "$args"
    invoke-expression -Command "$args"

and I use it in several places like traced cp "$($_.FullName)" (join-path $directory $newfile) so that I have a log of all of the places that get copied (or removed, or whatever)

But when the directory contains spaces and dashes, it results in invoke-expression throwing.

I guess I could just define traced-cp and traced-rm, but if I have a lot of functions I want to trace, what's the generic answer? I just want a function that prints, then evaluates, the exact command its given. From what I understand, the & operator isn't what I want here-- It won't work for shell builtins.

Why Invoke-Expression is Evil - Power Tips, *) What are alternatives for common scenarios where I might be resorting to Invoke-Expression, but don't need to? *) What are examples of where Invoke-  Invoke-Expression doesn’t help at all in this case, it just makes the problem more complicated. The bottom line: Invoke-Expression is a powerful and useful command for some scenarios such as creating new scripts at runtime, but in general, if you find yourself using Invoke-Expression, you should ask yourself, or maybe a respected colleague if

Consider using argument splatting to build your command instead of building a string-based command with Invoke-Expression. I also don't know where you heard that & doesn't work with shell built-ins but it works with both commands and cmdlets.

Here is the official Microsoft documentation on splatting in Powershell.

This approach also eliminates the difficulty in crafting a command string correctly, having to escape characters, and dealing with path spaces - using splatting with named or positional arguments takes care of most of this for you.

Invoke-Expression considered harmful, Some common reasons people try Invoke-Expression: It's slower than the alternatives; And maybe worst of all – it opens up a script to code  Invoke-Expression is used to execute whatever string is passed to it. This makes it very simple to run commands that don’t have Powershell equivalents. I started using it to run the 7zip command line executable when I needed to perform operations on zip files from within a script.

I would suggest using -verbose with copy-item or remove-item, and also -passthru with copy-item.

Invoke-Expression: Learning all about this PowerShell cmdlet, Alternative/Similar Cmdlets. Q. What's the difference between Invoke-Expression and the call operator ( & )?. A  The Invoke-Expression PowerShell cmdlet can be easy to misunderstand when and when not to use it. In this article, I've put together a number of top FAQs. I'm going to break them down and include tons of useful examples for you to reference whenever you might need them.

Richard Siddaway's Blog, We can also put the string into a variable. $command = "get-process" Invoke- Expression -Command $command. An alternative is to use the call  Cmdlet. Invoke-Expression cmdlet is used to perform a command or expression on local computer.. In these example, we're see the Invoke-Expression cmdlet in action. Example. In this example, we'll show how to invoke an expression.

Invoke-Command or Invoke-Expression, Invoke-Expression is used to execute whatever string is passed to it. This makes it very simple to run commands that don't have Powershell  The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression, a string submitted at the command line is returned (echoed) unchanged. Expressions are evaluated and run in the current scope. For more information, see about_Scopes. Caution Take reasonable precautions when using the Invoke-Expression

Call operator - Run - PowerShell, The call operator (&) allows you to execute a command, script or function. This usage (calling a script block) is similar to using Invoke-Expression to run a set  I try to avoid Invoke-Expression, but I have situations where I *think* I have to use Invoke-Expression as I don't have any alternatives. (FYI I think the answer of "Never use it" belies the reasons for which it was put into the language - there are times it is needed - whether I am doing it correctly or not)

  • Just turn on mandatory transcript logging on your machine! But the real question: Why do you want to use cmd builtins from PowerShell? :)
  • I want to use the powershell builtins, I just use the aliases cp and rm. But Start-Transcript looks like what I need, thanks!
  • If you're on Windows then there's also a policy setting you can use either locally or via GPO