Run Call - PowerShell


Overview

Run/Call is a PowerShell command that allows you to execute other programs or scripts within a PowerShell session. It provides a simple and convenient way to integrate external commands and automate complex tasks.

Syntax

Run [[-NoLogo] [-Wait] [-WindowStyle <style>] [-Encoding <encoding>] [-Args <arg-list>]] [<ScriptName>]
Call [[-NoLogo] [-Wait] [-WindowStyle <style>] [-Encoding <encoding>] [-Args <arg-list>]] [<ScriptBlock>]

Options/Flags

  • -NoLogo: Suppresses the display of the program’s splash screen.
  • -Wait: Suspends the current PowerShell session until the called program exits.
  • -WindowStyle: Specifies the window style for the called program: Normal, Hidden, Minimized, Maximized.
  • -Encoding: Sets the encoding of the input file for the script block.
  • -Args: Provides a comma-separated list of arguments to pass to the called script or program.

Examples

Run an external program:

Run notepad.exe

Run a script block asynchronously (without -Wait):

Call {Write-Host "Hello, world!"}

Run a script block synchronously (with -Wait):

$result = Call -Wait {Get-Process}

Common Issues

Permission Denied: Ensure the PowerShell session has sufficient permissions to run the called program or script.

Invalid Script Block: Check the syntax of the script block and ensure it is valid PowerShell code.

Integration

Combine with Start-Job: Run programs asynchronously and manage multiple jobs concurrently.

Start-Job {Call -Wait notepad.exe}

Pipe Output: Send the output of the Run/Call command to subsequent commands using the pipeline operator.

Run dir | Sort-Object LastWriteTime
  • Invoke-Expression
  • Invoke-Command
  • Start-Process