Solved

Enclosing cmdlet in parenthesis question

Posted on 2015-01-13
2
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 70

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 40

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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