Solved

Enclosing cmdlet in parenthesis question

Posted on 2015-01-13
2
163 Views
Last Modified: 2015-01-24
Hi,

I have a powershell question. At this stage of my powershell learning process I'm moving from cmdlet to scripting. I'm using a book at the moment and I'm trying to get my head around the putting the Cmdlet between parenthesis (Get-Host) & a . dot after it in powershell? I don't think the book explains it that well below. What happens here with the use of parenthesis does the get-host cmdlet run first? When it runs does it exact call the values/properties into memory for use. Are the results then put in the new object? Can somebody example this a bit better so i can understand what the code below is doing?

$PSConsoleWindow = (Get-Host).UI.RawUI

$PSConsoleWindow.BackgroundColor = "White"
$PSConsoleWindow.ForegroundColor = "DarkBlue"
$PSCOnsoleWindow.WindowTitle = "Jerry’s Customized PowerShell Console Session"

$PSConsoleSize = $PSConsoleWindow.WindowSize

$PSConsoleSize.Width = 80
$PSConsoleSize.Height = 25

$PSConsoleWindow.WindowSize = $PSConsoleSize

Clear-Host

The opening statement in this script uses the Get-Host cmdlet and its UI property to access the RawUI property. Note that the parentheses around the Get-Host cmdlet are required to ensure that the UI property reference is applied to the result returned by the cmdlet and not to the cmdlet itself. The result is that the first statement generates an object variable named $PSConsoleWindow, which can then be used to access and configure console properties.
0
Comment
Question by:makel2
2 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 40546481
As described,    (cmdlet).Property   is used to get the property of the cmdlet result.  cmdlet.Property does not work - this results in a syntax error.
(cmdlet).Property and cmdlet | select Property are equivalent.

With PS 2, this operation only works if you get a single object. You can't write (dir C:\Temp).FullName there. In PS 3 and above it is valid, however.

(get-date).AddDays(1) is another common example of using cmdlets in parens.
0
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 40546485
Putting the parentheses around the cmdlet is telling PS to run the cmdlet first.  Then from those results you can reference a property or method with a dot reference.  The following two examples are equivalent.
#example 1
(Get-Host).UI

#example 2
$results = Get-Host
$results.UI

Open in new window

The only difference here is that the second uses an intermediate variable.  In the first example the results from Get-Host are not kept in memory after the command, but in the second they will be until that variable is discarded.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question