Run String as Command in PowerShell

Run String as Command in PowerShell

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:

This will open new notepad window.

Here is more complicated example.

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 the CPU 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

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.

The above code is functioning in the following manner:

  • A string variable $filename is created, set to File1.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 the Get-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.

Using [scriptblock]::Create() Method

Use the [scriptblock]::Create() method to run a string as a command in PowerShell.

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 static Create() method of the ScriptBlock class. The Create 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 to Create() is the value of the $commandString variable, which is "Get-ChildItem C:\TEST1". This creates a script block representing the Get-ChildItem command with the argument C:\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 the Get-ChildItem command with the argument C:\TEST1, which retrieves information about the files and folders located in the C:\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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *