Параметризация функций

Quick Links

Key Takeaways

  • To run an executable file without admin rights, launch PowerShell, type the ampersand (&) symbol, press Spacebar, enter your EXE file’s path in double quotes, and press Enter.
  • To launch an EXE file with admin rights, use the “Start-Process FilePath “powershell” -Verb RunAs” command where FilePath is the full path enclosed with double quotes to your executable file.

Why Open EXE Files With PowerShell?

While the easiest way to launch files on Windows is to simply double-click your files, the usage of PowerShell to open files is handy in some cases.

For example, if your File Explorer utility isn’t working and restarting Windows Explorer hasn’t fixed the issue, you can use the PowerShell method to launch any executable file.

Another reason you may want to use a PowerShell command to launch files is if you’re writing a script. You may want to open a file in your script, which you can do by including the above PowerShell commands.

How to Launch an EXE File Without Admin Rights Using PowerShell

To start, open your PC’s Start Menu, find “PowerShell”, and launch the utility.

Windows 11's Start Menu with Windows PowerShell highlighted.

In PowerShell, type the ampersand (&) symbol, press Spacebar, enter your executable file’s path, and press Enter. If your file path has spaces, enclose the path with double quotes.

If you’re unsure how to locate your program’s file path, simply drag and drop your file onto your PowerShell window to automatically fill the file path. Another way to find a file’s path is to right-click the file, select “Properties,” open the “Details” tab, and look at the “Folder Path” value.

& "C:\Users\mahes\Desktop\My Files\FileZilla_3.62.2_win64-setup.exe"
A PowerShell window with a command that launches an EXE file on Windows 11.

PowerShell will launch your specified executable file, and you’re all set.

How to Launch an EXE File With Admin Rights Using PowerShell

Start-Process FilePath "powershell" -Verb RunAs

Although you only need to enclose the path with double quotes if the path has spaces, it’s a good practice to always use double quotes; it doesn’t cause any issues.

Start-Process "C:\Users\mahes\Desktop\My Files\FileZilla_3.62.2_win64-setup.exe" "powershell" -Verb RunAs
A PowerShell window with a command that opens an EXE file with admin rights on Windows 11.

  • A long-standing bug in PowerShell’s parameter binder when calling external programs, present up to at least v7.3.6, affecting --prefixed arguments that also contain . – see GitHub issue #6291.

    • Normally, the simplest workaround is to `-escape the initial -, but because mvn is a batch file this is not effective here, for the reasons explained below.

    • For background information and alternative workarounds, see this answer, but read on for why your specific workaround that involves nested quoting is needed.

  • mvn happens to be implemented as a batch file (mvn.cmd), and batch files inappropriately parse their arguments as if they had been passed from inside a cmd.exe session (a long-standing “quirk” that will not be fixed).

    • A simplified example:

      • Submitting mvn 'foo|bar' (or mvn "foo|bar" or mvn foo`|bar) in PowerShell constructs the process command line as mvn.cmd foo|bar, i.e. without quoting, which then breaks the batch file, due to the unquoted |.
    • GitHub issue #15143 proposed making PowerShell accommodate this problematic batch-file behavior by also double-quoting space-less arguments if they contain cmd.exe metacharacters, but, sadly, it was rejected.

:/>  Где включить блютуз на ноутбуке windows 8

While this bug is fixed in v7.3+, on Windows the old, broken behavior is by default selectively retained, notably when calling batch files, due to the $PSNativeCommandArgumentPassing preference variable defaulting to the ill-fated Windows mode.

A simple example:

  • The proper translation of PowerShell argument 'foo="bar"' for the process command line is "foo=\"bar\"" (or foo=\"bar\"), whereas the bug results in foo="bar" – which in your case happens to be what you actually need.

  • In PowerShell 7.3+, "foo=\"bar\"" is indeed what you get if you call any executable not covered by the selective exceptions, or if you’ve opted out of the exceptions via $PSNativeCommandArgumentPassing = 'Standard'

Note that Unix-like platforms are unaffected in v7.3+ (Standard is the default). The concept of a process-level command line (fortunately) doesn’t even exist on Unix-like platforms: instead, arguments are passed as an array of verbatim values.


  • The default value of $PSNativeCommandArgumentPassing on Windows may stay Windows forever, though the fact that backward compatibility in this area was already broken once, in v7.3, suggests that future changes are a possibilty.

  • By contrast, in Windows PowerShell (the legacy edition whose latest and last version is v5.1) the old, broken behavior that your workaround relies on is guaranteed to stay in place, given that Windows PowerShell is no longer actively developed and will receive only security-critical updates.

For PowerShell (Core) 7+, there are two future-proof workarounds – neither of them great:

Option A: (Temporarily) set $PSNativeCommandArgumentPassing to Legacy, which guarantees that the workaround that relies on the old, broken behavior will continue to work:

# Note the required use of embedded "..." (double-quoting)
# Enclosing the statements in & { ... } runs them in a *child scope*,
# which means that the $PSNativeCommandArgumentPassing change is limited to that scope.
& { $PSNativeCommandArgumentPassing = 'Legacy' mvn '-Dhttp.nonProxyHosts="xxx.xxx.*|*.example.com|*.example2.com|localhost|127.0.0.1"' -DskipTests clean install
}
  • cmd /c 'mvn -Dhttp.nonProxyHosts="xxx.xxx.*|*.example.com|*.example2.com|localhost|127.0.0.1"' -DskipTests clean install'
    • Note: This too relies on the "-escaping bug and only works because the “quirks” of cmd.exe‘s command-line parsing require embedded " chars. not to be escaped, i.e. cmd.exe‘s behavior cancels out PowerShell’s bug.

# Note the --% and the required use of "..." (double-quoting)
mvn --% -Dhttp.nonProxyHosts="xxx.xxx.*|*.example.com|*.example2.com|localhost|127.0.0.1" -DskipTests clean install

:/>  Управление назначенными заданиями средствами командной строки. Часть 5

Brajagopal Tripathi

Photo by Markus Spiske on Unsplash

There are two main ways to run a PowerShell function from the command line:

  • Using the & operator: The simplest way to run a PowerShell function from the command line is to use the ampersand (&) operator followed by the name of the function. For example, if you have a function called My-Function, you can run it by typing the following command into the command prompt:
  • Using the dot sourcing (.ps1) file: Another way to run a PowerShell function from the command line is to dot source (.ps1) the file that contains the function. To do this, type the following command into the command prompt:

Replace <filename> with the name of the file that contains the function. This will load the function into the current PowerShell session, and you can then run it by typing its name into the command prompt.

& My-Function -Name "John Doe"

In addition to these two methods, you can also run a PowerShell function from a command line by using the Invoke-Expression cmdlet. However, this method is not as common as the other two methods.

August 16th at 12:00am


Invoke-Expression is a language feature that allows for capturing the output of a command line application for parsing by your powershell script. If you are not careful, though, you could be plagued with messages like these:

For the last few months I’ve been using Invoke-Expression something like this:

$Action = "Certutil.exe –addstore –enterprise root $Certificate"
$result = Invoke-Expression $Action

This works fine if the EXE you are referencing is in one of the locations defined by the windows PATH Variable

:/>  Просмотр фотографий Windows 10: 5 лучших средств и где находятся, как открыть

If you attempt to run something like this, though, you will run into problems:

$Action = "c:\program files (x86)\nmap\nmap.exe -p 123 -sU -P0 time.windows.com"
$results = Invoke-Expression $Action

On the surface it seems fairly obvious what the next step should be: enclose the path to the exe in quotation marks taking care to escape them using the PowerShell back tick character ( ` ):

$Action = "`"c:\program files (x86)\nmap\nmap.exe`" -p 123 -sU -P0 time.windows.com"
$results = Invoke-Expression $Action

This puts us closer to our objective, but it still returns an error:

This error baffled me for about an hour before I found out what needed to be done next: prefix the double-quoted path to the EXE with an ampersand like this:

$Action = "&`"c:\program files (x86)\nmap\nmap.exe`" -p 123 -sU -P0 time.windows.com"
$results = Invoke-Expression $Action

Without the ampersand ( & )  character, Powershell parses the expression as a string rather than as a command. In this case we have to be explicit and tell Powershell that there is a command in there that we want to run. While it makes for some gnarly syntax, it does the trick.

Addendum (14-July-2012): I recently ran into a problem that requires a slight alteration to the solution provided above.

Problem

If you have an executable that requires a quoted or double quoted parameter you may be surprised to note that this seemingly obvious syntax does not work:

iex "&`"C:\Program Files\Vendor\program.exe`" -i -pkg=`"Super Upgrade`" -usr=User -pwd=password2"

The above-syntax simply escapes the double quotes around the parameter so they should be passed along to the EXE. Unfortunately you get this error:

powershell_error.png

Solution

The Baffling solution in my case was to triple-escape the double-quoted parameter. What makes this so confusing is that I only have to triple escape the closing quote:

iex "&`"C:\Program Files\Vendor\program.exe`" -i -pkg=`"Super Upgrade```" -usr=User -pwd=password2"