Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:54:47+00:00 2026-05-23T13:54:47+00:00

Using powershell, you can use the ‘&’ character to run another application and pass

  • 0

Using powershell, you can use the ‘&’ character to run another application and pass in parameters.

A simple example.

$notepad = 'notepad'
$fileName = 'HelloWorld.txt'

# This will open HelloWorld.txt
& $notepad $fileName   

This is good. But what if I want to use business logic to dynamically generate a command string? Using the same simple example:

$commandString = @('notepad', 'HelloWorld.txt') -join ' ';
& $commandString

I get the error:

The term ‘notepad HelloWorld.txt’ is
not recognized as the name of a
cmdlet, function, script file, or
operable program. Check the spelling
of the name, or if a path was
included, verify that the path is
correct and try again.

In my real example I’m trying to dynamically add or remove options to the final command line string. Is there a way I can go about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:54:47+00:00Added an answer on May 23, 2026 at 1:54 pm

    Two ways to do that:

    1. Separate the exe from the arguments

      Do all your dynamic stuff for the arguments, but call the exe as normal with the variable holding the arguments afterward:

      $exe = 'notepad'
      $argument = '"D:\spaced path\HelloWorld.txt"'
      &$exe $argument
      
      #or
      notepad $argument
      

      If you have more than one argument, you should make it an array if it will be separate from the exe part of the call:

      $exe = 'notepad'
      $arguments = '"D:\spaced path\HelloWorld.txt"','--switch1','--switch2'
      &$exe $arguments
      
    2. Use Invoke-Expression

      If everything must be in a string, you can invoke the string as if it were a normal expression. Invoke-Expression also has the alias of iex.

      $exp = 'notepad "D:\spaced path\HelloWorld.txt"'
      Invoke-Expression $exp
      

    In either case, the contents of the arguments and of the exe should be quoted and formatted appropriately as if it were being written straight on the commandline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using powershell, sometimes I want to only display for example the name or
How do I create an Application Pool on IIS 6.0 using a PowerShell script?
Using PyObjC , you can use Python to write Cocoa applications for OS X.
I am using PowerShell and am trying to run the following command: .\test_cfdp.exe <
I am trying to use the TFSSnapin in PowerShell from C# code using System.Management.Automation
I am writing some PowerShell scripts that use the MSDeploy API. I can load
I tried using this code in this thread: How can I uninstall an application
I'm using PowersHell to automate iTunes but find the error handling / waiting for
I'm trying to change a site's home directory using powershell. This is what I
I'm trying to modify the contents of an MSI file using Powershell. To do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.