How to Send Commands From LabVIEW to an Existing PowerShell Window

Updated Oct 1, 2026

Environment

Software

  • LabVIEW

Operating System

  • Windows

This article explains how to send commands from LabVIEW to an already open Microsoft PowerShell window that contains an active authenticated session. Use this method when you need to maintain credentials or session state across multiple command executions.
 
Standard LabVIEW methods such as System Exec.vi and .NET System.Diagnostics.Process standard input redirection create new processes and cannot attach to an existing interactive PowerShell console. To interact with an already open PowerShell window, use Windows Script Host through IWshRuntimeLibrary.IWshShell3 (WScript.Shell) and UI automation methods.
 
Prerequisites:
  • NI LabVIEW
  • Microsoft PowerShell
  • Windows Script Host (WScript.Shell)
  • ActiveX/COM support enabled on the target system
  • An authenticated PowerShell session already open

  1. Open Microsoft PowerShell manually.
  2. Complete the required authentication process and verify that the PowerShell session remains open.
  3. Create an ActiveX Automation Refnum for IWshRuntimeLibrary.IWshShell3 (WScript.Shell) in LabVIEW.
  4. Use the AppActivate method to activate the target PowerShell window.
  5. Specify the PowerShell window title as the application identifier. For example: "Windows PowerShell"
  6. Add a short delay after calling AppActivate to allow the PowerShell window to become the active foreground window.
  7. Use the SendKeys method to transmit the desired command string from LabVIEW.
  8. Append an Enter key sequence to execute the command: {ENTER}
  9. Trigger command transmission using your LabVIEW event structure, loop, or user interface logic.
  10. Repeat the SendKeys operation as needed while maintaining the PowerShell session.

You can execute multiple commands within the same authenticated PowerShell session without creating additional PowerShell processes or re-entering credentials.

The following example sends the command Get-Date to an existing PowerShell window:
 
  1. Activate the PowerShell window using AppActivate.
  2. Wait briefly to ensure the window receives focus.
  3. Send: "Get-Date"
     
  4. Send: {ENTER}
     
The command executes in the existing PowerShell session and returns the current date and time.
 
Code Snippet Disclaimer: The following code snippet is provided as an example implementation and may require modification for a specific application.
 
 
 
The following screenshot shows the command successfully executing in the target PowerShell window.
 
 
In the screenshot, the Get-Date command is sent from LabVIEW and executed in the existing PowerShell session, returning the current system date and time.

Next Steps

  • Add error handling for failed AppActivate calls.
  • Validate that the PowerShell window title is unique before activating the window.
  • Add configurable timing delays if command execution becomes unreliable.
  • Consider using a process ID instead of a window title when multiple PowerShell windows are open.