Windows cmd with arguments

Using command line arguments for Windows Terminal

You can use wt.exe to open a new instance of Windows Terminal from the command line. You can also use the execution alias wt instead.

If you built Windows Terminal from the source code on GitHub, you can open that build using wtd.exe or wtd .

Command line syntax

The wt command line accepts two types of values: options and commands. Options are a list of flags and other parameters that can control the behavior of the wt command line as a whole. Commands provide the action, or list of actions separated by semicolons, that should be implemented. If no command is specified, then the command is assumed to be new-tab by default.

To display a help message listing the available command line arguments, enter: wt -h , wt —help , wt -? , or wt /? .

Options and commands

Below is the full list of supported commands and options for the wt command line.

Option Description
—help , -h , -? , /? Displays the help message.
—maximized , -M Launches the terminal maximized.
—fullscreen , -F Launches the terminal as full screen.
—focus , -f Launches the terminal in the focus mode. Can be combined with maximized .
—window , -w Launches the terminal in a specific window.
Command Parameters Description
new-tab , nt —profile, -p profile-name , —startingDirectory, -d starting-directory , commandline , —title , —tabColor Creates a new tab.
split-pane , sp -H, —horizontal , -V, —vertical , —profile, -p profile-name , —startingDirectory, -d starting-directory , —title , —tabColor , —size, -s size , commandline , -D, —duplicate Splits a new pane.
focus-tab , ft —target, -t tab-index Focuses on a specific tab.
move-focus , mf direction Move focus between panes in the given direction. Accepts one of up , down , left , right .

When opening Windows Terminal from cmd (Command Prompt), if you want to use your custom «cmd» profile settings, you will need to use the command wt -p cmd . Otherwise, to run your default profile settings, just use wt cmd .

The -D, —duplicate parameter for split-pane is only available in Windows Terminal Preview.

Command line argument examples

Commands may vary slightly depending on which command line you’re using.

Target a specific window

The ability for the —window,-w parameter to accept window names is only available in Windows Terminal Preview.

Below are examples of how to target specific windows using the —window,-w option.

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running.

Open a new profile instance

To open a new terminal instance, in this case the command will open the profile named «Ubuntu-18.04», enter:

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running.

The -p flag is used to specify the Windows Terminal profile that should be opened. Substitute «Ubuntu-18.04» with the name of any terminal profile that you have installed. This will always open a new window. Windows Terminal is not yet capable of opening new tabs or panes in an existing instance.

Target a directory

To specify the folder that should be used as the starting directory for the console, in this case the d:\ directory, enter:

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running.

Multiple tabs

To open a new terminal instance with multiple tabs, enter:

PowerShell uses a semicolon ; to delimit statements. To interpret a semicolon ; as a command delimiter for wt command-line arguments, you need to escape semicolon characters using backticks. PowerShell also has the stop parsing operator (—%), which instructs it to stop interpreting anything after it and just pass it on verbatim.

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running.

To open a new terminal instance with multiple tabs, in this case a Command Prompt profile and a PowerShell profile, enter:

PowerShell uses a semicolon ; to delimit statements. To interpret a semicolon ; as a command delimiter for wt command-line arguments, you need to escape semicolon characters using backticks. PowerShell also has the stop parsing operator (—%), which instructs it to stop interpreting anything after it and just pass it on verbatim.

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and the \; backslash + semicolon separates commands.

Multiple panes

To open a new terminal instance with one tab containing three panes running a Command Prompt profile, a PowerShell profile, and your default profile running a WSL command line, enter:

PowerShell uses a semicolon ; to delimit statements. To interpret a semicolon ; as a command delimiter for wt command-line arguments, you need to escape semicolon characters using backticks. PowerShell also has the stop parsing operator (—%), which instructs it to stop interpreting anything after it and just pass it on verbatim.

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and the \; backslash + semicolon separates commands.

The -H flag (or —horizontal ) indicates that you would like the panes to be split horizontally. The -V flag (or —vertical ) indicates that you would like the panes split vertically.

Multiple tabs and panes

The new-tab and split-pane commands can be sequenced to get multiple tabs, each with split panes. To open a new terminal instance with two tabs, each with two panes running a Command Prompt and a WSL command line, with each tab in a different directory, enter:

PowerShell uses a semicolon ; to delimit statements. To interpret a semicolon ; as a command delimiter for wt command-line arguments, you need to escape semicolon characters using backticks. PowerShell also has the stop parsing operator (—%), which instructs it to stop interpreting anything after it and just pass it on verbatim.

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and the \; backslash + semicolon separates commands. Note to specify a Windows directory as the starting directory for wsl.exe that two backslashes \\ are required.

Tab title

To open a new terminal instance with custom tab titles, use the —title argument. To set the title of each tab when opening two tabs, enter:

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and the \; forward-slash + semicolon separates commands.

Using application title (Preview)

To open a new terminal instance allowing applications within it to set the tab title by sending title change messages, use the —useApplicationTitle flag. To suppress these messages, use the —suppressApplicationTitle flag. If none of these flags are provided, the behavior is inherited from the profile’s settings. To open a tab with title tabname that will not be overridden by the application, enter:

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and the \; backslash + semicolon separates commands.

This feature is only available in Windows Terminal Preview.

Tab color

To open a new terminal instance with custom tab colors, use the —tabColor argument. This argument overrides the value defined in the profile, but can be overridden as well using the tab color picker. In the following example, a new terminal is created with two tabs of different colors:

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and \; separates commands.

When —tabColor is set for a tab, it is associated with the first pane of this tab. Hence in a tab with multiple panes, the color will be applied only if the first pane is in focus. To set the tab color for additional panes, you will need to add the —tabColor parameter to the split-pane subcommand as well. In the example below, a tab with two panes is created with tab colors specified for each pane:

Color scheme (Preview)

To open a new terminal instance with a specific color scheme (instead of the colorScheme set in the profile), use the —colorScheme argument. This argument overrides the value defined in the profile.

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and \; separates commands.

This feature is only available in Windows Terminal Preview.

Tab focus

To open a new terminal instance with a specific tab in focus, use the -t flag (or —target ), along with the tab-index number. To open your default profile in the first tab and the «Ubuntu-18.04» profile focused in the second tab ( -t 1 ), enter:

Execution aliases do not work in WSL distributions. If you want to use wt.exe from a WSL command line, you can spawn it from CMD directly by running cmd.exe . The /c option tells CMD to terminate after running and the \; backslash + semicolon separates commands.

Examples of multiple commands from PowerShell

Windows Terminal uses the semicolon character ; as a delimiter for separating commands in the wt command line. Unfortunately, PowerShell also uses ; as a command separator. To work around this, you can use the following tricks to run multiple wt commands from PowerShell. In all the following examples, a new terminal window is created with three panes — one running Command Prompt, one with PowerShell, and the last one running WSL.

The following examples use the Start-Process command to run wt . For more information on why the terminal uses Start-Process , see Using start below.

Single quoted parameters

In this example, the wt parameters are wrapped in single quotes ( ‘ ). This syntax is useful if nothing is being calculated.

Escaped quotes

When passing a value contained in a variable to the wt command line, use the following syntax:

Note the usage of ` to escape the double-quotes ( » ) around «Windows PowerShell» in the -p parameter to the split-pane parameter.

Using start

All the above examples explicitly used start to launch the terminal.

The following examples do not use start to run the command line. Instead, there are two other methods of escaping the command line:

  • Only escaping the semicolons so that PowerShell will ignore them and pass them straight to wt .
  • Using —% , so PowerShell will treat the rest of the command line as arguments to the application.

In both of these examples, the newly created Windows Terminal window will create the window by correctly parsing all the provided command-line arguments.

However, these methods are not recommended currently, as PowerShell will wait for the newly-created terminal window to be closed before returning control to PowerShell. By default, PowerShell will always wait for Windows Store applications (like Windows Terminal) to close before returning to the prompt. Note that this is different than the behavior of Command Prompt, which will return to the prompt immediately.

How-to: Pass Command Line arguments (Parameters) to a Windows batch file.

A command line argument (or parameter) is any value passed into a batch script:

C:> MyScript.cmd January 1234 «Some value»

Arguments can also be passed to a subroutine with CALL:

CALL :my_sub 2468

You can get the value of any argument using a % followed by it’s numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on

%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 . %255)
only arguments %1 to %9 can be referenced by number.

Parameter Extensions

When an argument is used to supply a filename then the following extended syntax can be applied:

we are using the variable %1 (but this works for any parameter)

f1 Expand %1 to a Fully qualified path name — C:\utils\MyFile.txt

%

d1 Expand %1 to a Drive letter only — C:

p1 Expand %1 to a Path only e.g. \utils\ this includes a trailing \ which will be interpreted as an escape character by some commands.

n1 Expand %1 to a file Name without file extension or path — MyFile
or if only a path is present, with no trailing backslash, the last folder in that path.

x1 Expand %1 to a file eXtension only — .txt

s1 Change the meaning of f , n , s and x to reference the Short 8.3 name (if it exists.)

a1 Display the file attributes of %1

t1 Display the date/time of %1

z1 Display the file size of %1

$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

The modifiers above can be combined:

dp1 Expand %1 to a drive letter and path only

sp1 Expand %1 to a path shortened to 8.3 characters

nx2 Expand %2 to a file name and extension only

These parameter/ argument variables are always denoted with a single leading %
This is unlike regular variables which have both leading and trailing %’s such as %variable% , or FOR command variables which use a single leading % on the command line or a double leading %% when used in a batch file.

Parameter expansion will treat a Full Stop within a directory name as a file extension, so for a name like » Sample 2.6.4 » the output of %

n1 will be truncated to » Sample 2.6 » to return the whole folder name use %1 or %

If the output of DIR, or a filename with no drive letter/path is expanded to display a Full pathname %

f1 the command shell will assume; often incorrectly; that the file resides in the current directory. The CMD shell does not read file descriptors, or work with filesystem objects the way that PowerShell does, it is processing a simple list of text strings.

When writing batch scripts it’s a good idea to store the values in a named variable SET _LogFile=%

dp1 , the rest of the script can then refer to the easy-to-read variable name %_LogFile% This will also make life easier if you later need to change around the order of the parameters.

Tokenization

Tokenization of the command line arguments is not always done by the cmd.exe shell. Most often the tokenization is done by the newly formed processes’ runtime, at the OS level, Windows passes a command line untokenized as a single string to the new process. You can read more about the low-level details of the batch language/cmd escaping from the excellent answers by jeb and dbenham on this StackOverflow question.

Passing by Reference

In addition to passing numeric or string values on the command line, it is also possible to pass a variable name and then use the variable to transfer data between scripts or subroutines. Passing by reference is a slightly more advanced technique but can be particularly useful when the string contains characters that are CMD delimiters or quotes.

You can get the pathname of the batch script itself with %0 , parameter extensions can be applied to this so %

dp0 will return the Drive and Path to the batch script e.g. W:\scripts\ and %

f0 will return the full pathname W:\scripts\mybatch.cmd

You can refer to other files in the same folder as the batch script by using this syntax:

This can even be used in a subroutine, Echo %0 will give the call label but, echo «%

nx0″ will give you the filename of the batch script.

When the %0 variable is expanded, the result is enclosed in quotation marks.

a1 to display the Extended Attributes of a file.

aI recognizes 9 NTFS file attributes. The expansion of a file attribute produces a series of 9 dashes, with each recognized attribute replacing a dash with a letter. A file with no recognized attributes or with none set will expand to 9 dashes like this: ———

Other NTFS attributes not recognised by %%

aI can be read using FSUTIL usn command:
FILE_ATTRIBUTE_ENCRYPTED
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
FILE_ATTRIBUTE_SPARSE_FILE

Example: Expansion of a file with the Hidden and System attributes:
—hs—-

Maximum line length

The maximum length of any command line (or variable) within CMD is 8191 characters.

Terminology: Command-line Argument or Parameter?

In mathematical terms a parameter is «a numerical or other measurable factor» so typically a number.

In programming terminology:
A parameter represents a value that the procedure expects you to pass when you call it.
An argument represents the actual value that you pass to a procedure.

In practice the phrases argument and parameter tend to be used interchangeably, CMD batch files do not perform any type checking.

FOR parameters

The FOR command creates parameter variables which are identified with a letter rather than a number (e.g. %%G ).
The Parameter Expansions described above can also be applied to these.
To avoid confusion between the two sets of letters, avoid using the letters (a, d, f, n, p, s, t, x, z) as FOR parameters or just choose a FOR parameter letter that is UPPER case.
So for example in a reference like %%

fG the %%G is the FOR parameter, and the

f is the Parameter Expansion.

Pass parameters from one batch to another:

Or as part of a CALL :

Get the file size of C:\demo\sample.xlsx using the %

z parameter extension:

Passing values from one part of a script to another:

“I came here for a good argument, No you didn’t, you came here for an argument”

Читайте также:  Run linux on freebsd
Оцените статью