Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enclosing cmdlet in parenthesis question

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

719 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