How to Invoke Expressions Using PowerShell
-
the
Invoke-Expression
in PowerShell -
Pass Parameters to Scripts With
Invoke-Expression
in PowerShell -
Difference of
Invoke-Expression
From the Call Operator in PowerShell -
Difference of
Invoke-Command
FromInvoke-Expression
in PowerShell
The Invoke-Expression
PowerShell cmdlet can easily be misunderstood when and when not to use it. This article will teach us how Invoke-Expression
works and how we can put it into practical use.
the Invoke-Expression
in PowerShell
The Invoke-Expression
command evaluates or runs a specific string as a form of script block and returns the results of the command or expression. It can help call the code within a script, build commands to be executed later, or use it cautiously with user-provided input.
One example of Invoke-Expression
is defining a script and passing that string to the Command
parameter.
Script Sample:
$getProcess = 'Get-Process'
Invoke-Expression -Command $getProcess
$pathFile = '.\Test.ps1'
Invoke-Expression -Command $pathFile
Invoke-Expression
will execute the script as expected if you enclose the path item in double ""
or single quotes''
with the entire string in quotes. You can also indicate the full path location of the script file like the sample below.
Script Sample:
$pathFile = "C:\'Folder Path'\Test.ps1"
Invoke-Expression $pathFile
Pass Parameters to Scripts With Invoke-Expression
in PowerShell
There is no native method to pass parameters with Invoke-Expression
, but we can include them in the string you give to the Command
parameter. Instead of using a parameter via Invoke-Expression
, we can pass parameters to that script by giving them.
We have to include that entire line in a string and then pass that string to the Command
parameter.
Script Sample:
$pathFile = 'C:\Scripts\Test.ps1'
$scriptParam = '-Path "C:\file.txt" -Force'
Invoke-Expression "$pathFile $scriptParam"
# or
$pathFile = 'C:\Scripts\Test.ps1 -Path "C:\test_file.txt" -Force'
Invoke-Expression $pathFile
Difference of Invoke-Expression
From the Call Operator in PowerShell
The call operator (&
) is used to execute a script, script block, or command, but it does not interpret command
parameters as Invoke-Expression
does.
For example, we will get the PowerShell Core process using the Get-Process -ProcessName pwsh
cmdlet. Unfortunately, using the call
operator, concatenating Get-Process
and the parameter will not work.
Script Sample:
$getProc = "Get-Process"
## The script below will not work
& "$getProc pwsh"
The command will execute if we run this string with Invoke-Expression
as shown below.
Script Sample:
Invoke-Expression "$getProc pwsh"
Difference of Invoke-Command
From Invoke-Expression
in PowerShell
Only a string is converted to executable code by the Invoke-Expression
cmdlet. In contrast, the Invoke-Command
command allows us to run programs on computers remotely or locally.
The Invoke-Command
cmdlet is preferable if we are writing the executed commands now, as we retain IntelliSense in our IDE, whereas Invoke-Expression
would be preferable if we wanted to call another script from within our current one.
Script Sample:
## Invoke-Command
Invoke-Command -ScriptBlock {
Get-Process PowerShell
}
## Invoke-Expression
Invoke-Expression -Command "
Get-Process PowerShell
"
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn