Skip to main content

How to Use a Command's Result as an Argument with Parentheses in PowerShell

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

To give one command's result to another command, put the first command in parentheses, as in Test-Path (Join-Path $HOME 'Documents'). This guide runs the inner command on its own, passes its result to Test-Path, and shows what PowerShell does with the same words when the parentheses are missing, with real output from PowerShell 7.6.10.

The about_Operators page, on the grouping operator: "(...) allows you to let output from a command participate in an expression."

Syntax​

Syntax
<command> (<inner command>)
<command> -<Parameter> (<inner command>)
PartWhat it means
<command>The command that receives the value, such as Test-Path
(<inner command>)The command whose result you want to pass. PowerShell runs it, and its result becomes the argument
-<Parameter>Optional: the name of the parameter the result goes to

The Example​

The example checks whether a Documents folder exists in your home folder. Join-Path $HOME 'Documents' builds the folder's path, and Test-Path checks it. The commands only read, so they change nothing. They run in the PowerShell 7.6.10 console on a Mac.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.

Step 1: Run the Inner Command on Its Own​

PowerShell
Join-Path $HOME 'Documents'
Output
/Users/you/Documents

$HOME holds the full path of your home folder, and Join-Path added the name Documents to it. Your own user name appears where this shows you. This path is the value the next step passes on.

Step 2: Pass Its Result to Another Command​

PowerShell
Test-Path (Join-Path $HOME 'Documents')
Test-Path (Join-Path $HOME 'no-such-folder')

macOS-style code window titled PowerShell with two Test-Path commands, each given the result of a Join-Path command in parentheses: one builds the path of the Documents folder in the home folder, the other the path of no-such-folder.

Output
True
False

PowerShell ran each Join-Path command in parentheses, and Test-Path received the path it returned. The home folder has a Documents folder, so the first check returned True; it has no folder named no-such-folder, so the second returned False. The Test-Path page: "It returns $true if all elements exist and $false if any are missing."

Step 3: See Why the Parentheses Are Needed​

Write-Output prints each value it's given on its own line, so it shows exactly what a command receives. The Write-Output page: "If Write-Output is the last command in the pipeline, the objects are displayed in the console."

PowerShell
Write-Output Join-Path $HOME 'Documents'
Write-Output (Join-Path $HOME 'Documents')
Output
Join-Path
/Users/you
Documents
/Users/you/Documents

macOS-style terminal window titled PowerShell where Write-Output Join-Path $HOME 'Documents' prints three lines, Join-Path, /Users/you and Documents, and the same command with the Join-Path part in parentheses prints one line, /Users/you/Documents.

Without parentheses, Write-Output received three separate values: the words Join-Path, the home folder and Documents. Join-Path never ran; PowerShell read its name as plain text. With parentheses, Write-Output received one value, the path that Join-Path returned.

The about_Parsing page explains why. After a command's name, PowerShell reads the rest of the line in argument mode: "All input is treated as an expandable string unless it uses one of the following syntaxes:", and one of them is "Parentheses (()) begin a new expression".

Step 4: Pass the Result to a Named Parameter​

PowerShell
Test-Path -Path (Join-Path $HOME 'Documents') -PathType Container
Output
True

Parentheses work after a parameter's name too. The path went to -Path, and -PathType Container made Test-Path also check that the path leads to a folder.

Common Mistakes​

MistakeWhat happensDo this instead
No parentheses: Test-Path Join-Path $HOME 'Documents'Test-Path: A positional parameter cannot be found that accepts argument '/Users/you'. The text Join-Path became the path, and the home folder had no parameter to go toPut the inner command in parentheses
Double quotes instead of parentheses: Test-Path "Join-Path $HOME 'Documents'"False: the whole text became the path, and nothing has that nameUse parentheses
Braces instead of parentheses: Test-Path {Join-Path $HOME 'Documents'}Test-Path: Cannot evaluate parameter 'Path' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input.Use parentheses

The about_Parsing list explains the last two rows: "Quotation marks (' and ") begin strings", and "Braces ({}) begin a new scriptblock".

FAQ​

Can I save the result in a variable first?​

Yes, which reads more easily when the inner command is long: $docs = Join-Path $HOME 'Documents' saved the path, and Test-Path $docs returned True.

Does $( ) work as well as ( )?​

For one command, it gave the same answer: Test-Path $(Join-Path $HOME 'Documents') returned True. The about_Operators page on $( ), the subexpression operator: "Returns the result of one or more statements." And: "Use this when you want to use an expression within another expression."

Do parentheses work for calculations too?​

Yes, by the same rule: Write-Output 2+2 printed 2+2, and Write-Output (2+2) printed 4. The about_Parsing page lists this pair among its examples of argument mode and expression mode.

Conclusion​

Test-Path (Join-Path $HOME 'Documents') returned True: the parentheses made PowerShell run Join-Path and pass its result on. Without them, the command's name is only text. Put the inner command in parentheses, before or after a parameter's name, or save its result in a variable first.

Additional Resources​

  • about_Operators — the grouping operator and the subexpression operator.
  • about_Parsing — argument mode, expression mode, and the characters that switch between them.
  • Test-Path — the command that checks the path in this example.