Table of Contents
Using Invoke-Expression
Cmdlet
Use the Invoke-Expression
cmdlet to run a string as a command in PowerShell. Invoke-Expression cmdlet evaluates string as command and return the result of string expression.
Here is simple exampple:
1 2 3 4 |
$command = "notepad.exe" Invoke-Expression $command |
This will open new notepad window.
Here is more complicated example.
1 2 3 4 |
$command = "Get-Process | Sort-Object -Property CPU -Descending | Select-Object -First 5" Invoke-Expression -Command $command |
1 2 3 4 5 6 7 8 9 |
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1456 205 329656 294348 670.89 6328 3 Grammarly.Desktop 2564 67 326064 354484 503.28 12276 3 chrome 524 24 776508 798760 444.05 1248 3 chrome 644 17 7196 21308 400.55 12324 3 SynTPEnh 1420 41 315816 308592 394.67 8188 3 chrome |
The Invoke-Expression
evaluates a string as a PowerShell expression, allowing you to execute any valid PowerShell command or script. For example, the above code is functioning in the following manner:
Get-Process
: Retrieves a list of currently running processes on the system.Sort-Object -Property CPU -Descending
: Sorts the list of processes by theCPU
property (which represents the CPU usage for each process) in descending order.Select-Object -First 5
: Select the first five items from the sorted list of processes.
After defining the command in the $command
variable, we use the Invoke-Expression
cmdlet to execute the command stored in the $command
. The output of this code is a list of the top 5 processes with the highest CPU usage, sorted in descending order.
Using &
Operator
Use the &
operator to run a string as a command in PowerShell
1 2 3 4 |
$command = "notepad.exe" & $command |
The call operator &
invokes a command as if it were a command name or function. This allows you to execute a command stored in a variable or run a command that is not in the current scope or path. For example, the above code is functioning in the following manner:
- A string variable
$command
is initialized with an executable file, in this case,notepad.exe
. - After that, the call operator (
&
) was used to execute the command stored in$command
. This tells PowerShell to run the executable specified in the$command
. In this case, it has launched the Notepad application.
Using Double Quotes (""
)
Use the Double Quotes
to run a string as a command in PowerShell.
1 2 3 4 5 |
$filename = "File1.txt" $command = "Get-Process | Out-File -FilePath <code>"E:\$filename</code>"" Invoke-Expression -Command $command |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 507 20 8804 16768 13328 0 AppHelperCap 773 43 36872 48716 9.02 1212 3 ApplicationFrameHost 318 17 3700 14256 3388 0 armsvc 315 16 105520 19700 0.34 3760 0 audiodg 555 24 409364 392232 505.98 1248 3 chrome 399 32 24192 47240 120.28 2476 3 chrome 255 16 18168 35456 0.23 5224 3 chrome 260 17 43572 65600 18.72 7476 3 chrome 1335 33 244596 226076 508.02 8188 3 chrome ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... |
The above code is functioning in the following manner:
- A string variable
$filename
is created, set toFile1.txt
. - The
$command
variable is then set to a string that contains a PowerShell command to get a list of running processes and write them to a file with the specified filename on the E: drive. - The
Out-File
cmdlet is used to write the output of theGet-Process
cmdlet to a file with the specified filename. The backticks (`) escape the double quotes within the string. - The
Invoke-Expression
cmdlet is then used to execute the command stored in the$command
variable. - The output of this code is a file named
File1.txt
located in the E: drive, which contains a list of running processes when the command was executed.
Further reading:
Using [scriptblock]::Create()
Method
Use the [scriptblock]::Create()
method to run a string as a command in PowerShell.
1 2 3 4 5 |
$commandString = "Get-ChildItem C:\TEST1" $commandBlock = [scriptblock]::Create($commandString) & $commandBlock |
1 2 3 4 5 6 7 8 |
Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2/21/2023 11:26 PM FolderA d----- 2/21/2023 11:27 PM FolderB -a---- 2/21/2023 9:32 PM 0 File1.txt -a---- 2/21/2023 9:32 PM 0 File2.txt |
The above code is functioning in the following manner:
- The first line of the code block creates a variable called
$commandString
and sets its value to"Get-ChildItem C:\TEST1"
. - After that, the second line creates a script block object called
$commandBlock
using the staticCreate()
method of theScriptBlock
class. TheCreate
method takes a string as input and returns a script block object that can be executed as a PowerShell command. In this case, the string passed toCreate()
is the value of the$commandString
variable, which is"Get-ChildItem C:\TEST1"
. This creates a script block representing theGet-ChildItem
command with the argumentC:\TEST1
. - Finally, in the last line of code, the call operator (
&
) was used to execute the script block stored in the$commandBlock
variable. The call operator tells PowerShell to treat the script block as a command and run it. The script block, in turn, executes theGet-ChildItem
command with the argumentC:\TEST1
, which retrieves information about the files and folders located in theC:\TEST1
directory.
Considering the above solutions, running string as a command in PowerShell is a powerful tool that can help you automate complex tasks and perform advanced system administration tasks. Furthermore, following best practices, you can use this feature securely and efficiently to manage your Windows systems.
That’s all about how to run string as command in PowerShell.