Solved

Enclosing cmdlet in parenthesis question

Posted on 2015-01-13
2
158 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 68

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Set OWA language and time zone in Exchange for individuals, all users or per database.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now