Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Enclosing cmdlet in parenthesis question

Posted on 2015-01-13
2
Medium Priority
?
200 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 71

Accepted Solution

by:
Qlemo earned 1000 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 41

Assisted Solution

by:footech
footech earned 1000 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

Problems using Powershell and Active Directory?

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

963 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