Q. What are parentheses used for in PowerShell?
April 30, 2010
A. In PowerShell, parentheses are used to group expressions. If you’re looking at a complicated command that includes parentheses, there’s an easy trick to figuring out what’s going on. Consider this example:
Get-WmiObject Win32_BIOS -computerName (Get-Content c:ames.txt)
Here, the parenthetical expression, Get-Content c:ames.txt, will be executed first. The entire parenthetical expression will be replaced by whatever the expression resulted in. For example, if Get-Content is reading a text file that contains a bunch of computer names, the new command will be something like this:
Get-WmiObject Win32_BIOS -computerName server1,server2,server3,server4
Those computer names—the results of whatever was happening inside the parentheses—will become the set of values that is passed to the -computerName parameter. In fact, you’ll often see parentheses used to execute one command and pass its results to a parameter of another command.
About the Author
You May Also Like