Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Difference of Invoke-Expression and '&' in PowerShell

# Command Result
1 Invoke-Expression 'Get-Process' OK
2 & 'Get-Process' OK
3 Invoke-Expression 'Get-Process explorer' OK
4 & 'Get-Process explorer' NG

#4 causes the following error:

& : 用語 'Get-Process explorer' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認
識されません。名前が正しく記述されていることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、再試
行してください。
発生場所 行:1 文字:3
+ & 'Get-Process explorer'
+   ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-Process explorer:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException