Get-Member gets the members (the properties and methods) of objects. Obviously, to do that it needs an object to to process. You can either pipe the object to the cmdlet, as you did in your first example, or use the -InputObject parameter:
You use the Get-Member cmdlet to get the properties and methods of an object.
Your first example: get-date | get-member -name date first runs the Get-Date cmdlet and sends the result (a DateTime object) to the “Success” stream, which you direct to a pipeline. The Get-Member cmdlet receives the DateTime object from the pipeline and sends the contents of the DateTime objects “date” property into the success stream. The success stream is, by default, directed to a formatter for display).
Your second example: get-member -name date receives no input object at all. That situation results in the error message “Get-Member: You must specify an object for the Get-Member cmdlet.”
If you’re looking to learn how to think in terms of using pipelines (and objects flowing through them) I’d suggest starting off with a few books and online reading/practice:
Learn PowerShell in a Month of Lunches (4th Edition, 2025)
PowerShell in Depth (covers version 4.0, but has exercises that are still relevant)
PowerShell in Action (3rd Edition, covers some PowerShell in addition to Windows PowerShell)
Microsoft Learn — PowerShell Fundamentals (on-line)
Practice, practice, practice, practice (try www.exercism.ORG and don’t be discouraged if you encounter advances exercises that you may never have need for, you can skip those. Mentoring is also available – best of all it’s free)
@alan.revera I’m sorry to see that you chose to delete your first reply. While the contents of it were legitimate (in my opinion) it was slightly off-topic (to this question). Perhaps asking another question with a different topic would be a better choice?
I’ve always wondered why the folks that write books about PowerShell often cover the “how” and not the “why” of the language. I’ve always based my comparisons on the “Programming Perl” book (a.k.a. the Camel book). It’s author (a linguist) laid out both the “how” and “why” at the same time, and wrote not only factually, but intermixed the facts with humor, trivia, and how the language evolved.
I am glad I am not the only one feeling that way. I deleted the posts because I got my answer. Google is the magic. I finally type the right question and google actually gave me the right answer. It’s very simple, they just have a way to put {} or () or logic functions like -and, -or etc at the last one on the line and Powershell will recognize that not the end of the line and start from a new line. I even wrote my own notes on this. You can see the page in the attachment.
Now I moved onto Read-Host, asking numbers and use that in the if elseif else script. This is fun.
I’m surprised that the answer omitted the simplest way: end the line with a back-tic followed immediately with a CrLf (“enter”). That acts as a line continuation character. No need to balance parenthesis or curly braces.
You’ll be happier to work with a good editor or ISE. If you’re using Powershell (not Windows PowerShell where the ISE delivered with PowerShell and works well enough) you can use VS Code (it’s free and open source) or the free version of Visual Studio,
Keep in mind that both Windows PowerShell and PowerShell are shells, not compilers or interpreters. They, like com.exe, bash, and other shells, are line-oriented.
Don’t forget Exercism.org. the exercises there are very helpful when learning a new language.
One last parting mention: keep in mind that PowerShell is built around streaming, and there are things that will take you by surprise, like this common (be ultimately fatal) expression:
If ($MyArray -eq $null){do something}
It’s the individual items in the array that will be examined, not the array! The contents of $MyArray will be streamed into the “-eq” operator!
I have a question. I am experimenting a program to ask to enter a number into “$input”. Then compare and print out. This is my program. It doesn’t work. From reading, it only execute the first read-host and stop and ignore the rest. How should I write it to make it work?
$input is in PowerShell an automatic variable. You should not use it for your own variables.
Here you can read more about:
= is in PowerShell the assignment operator. If you want to compare something - maybe a variable to a value - you should use comparison operators like -eq.
Here you can read more about:
Try it this way:
$A = 10
$B = 20
$userInput = Read-Host -Prompt 'Enter a number!'
If ($userInput -eq $A) {
Write-Host 'Input is 10.'
} elseif ($userInput -eq $B) {
Write-Host 'Input is 20'
} else { Write-Host 'no match'}
It seems like you have an unusual character in front of your parameter name Prompt of your cmdlet Read-Host. This may cause the actual issue in your code.
I’d highly recommend using a modern code editor like VSCode. There you would get a hint that you use non standard characters.
To make your program work you need to have it enter, and repeat, a loop until you determine it should leave the loop. Your corrected program is below, and it will stay in the “while” loop forever, or until the user input satisfies one of the two conditions you’ve stipulated.
If this were a script that was meant for general use I suppose you’d want to limit the number of wrong guesses. You would also want to verify that the data accepted from the console contained a whole number and no other characters except decimal digits.
Also, you’ve used an assignment operator (“=”) where you should have used a comparison operator (e.g. “-eq”).
$A=10
$B=20
# $input is one of PowerShells "automatic variables"*
# $input contains the current object in the pipeline. It is equivalent to $PSItem.*
# Avoid reassigning its value or else risk messing up pipeline processing*
# Since there is no pipeline in this script, its use here is harmless, but get in the
# habit of not changing it*
#$input = read-host -prompt " enter a number"*
$guess = 0 # changed $input to $guess
while($true){
$guess = read-host -prompt "enter a number"
if($guess -eq $A) { # the "=" is an assignment operator. "-eq" is a logic operator
write-host "guess is 10."
break
}
elseif ($guess -eq $B) {
write-host "guess is 20."
break
} else {
write-host "no match"
}
}
This works because of “order of operations” I think that’s fine while at the prompt but I would probably never have a need to use that in an actual script.
Thanks to you, you made me get up 4:30AM to chase after the “WILD” loop for over an hour!!! Also read-host and write-host.
I think I got it working, just a question about how it displays. Here is the program:
while ($true){
$name = read-host -prompt " enter name "
break}
write-host " you name is " $name
attached is the display. My question is those program lines that shows. It’s not like normal program that when you run, it ask you the name. Then you type the name and it will show “Your name is ***”
Here is the screen capture. Anyway to no show those program lines? Thanks.
As I said before, PowerShell is a shell. You’ll note that the prompt on the first line of that display is “PS C:\Users\yungm>” – it ends with a single “>” character that denotes the shell is waiting for the first (and possibly the only) line of input. That first line ends with a left curly brace “{”, and the shell expects there to be more data between that and a right curly brace “}”. To signal to you that you haven’t finished what you started, the prompt changes to “>>”. The third line (still being in the unfinished line (it hasn’t seen the closing curly brace yet) also uses the “>>” prompt. At the end of the third line there’s a right curly brace, which signals that the command is complete. At that time, the shell executes the completed statement and the read-host cmdlet runs putting your " enter name " prompt on the display.
Is there a way to not show the “program lines”? Yes . . . use a text editor and put the lines into a file. Save the file using the suffix “.ps1”. Then, from the PowerShell shell, change to the directory in which you saved that ps1 file, then (from the prompt) enter “.\filename.ps1” and then the “Enter” key.
FWIW, you didn’t need to use a while-loop (or any other looping code) to execute code that runs only once and then exits.
If you’re running Windows PowerShell (probably version 5), there’s a fairly robust IDE installed with Windows PowerShell. Have a look at your Windows “Start” menu and you should see it in the “PowerShell” group.
Your post taught me a lot on how to look at “>>” vs “>”. I don’t even know where to start on these. Like I said, I have 2 books, it’s all over the place, pretty much useless to follow.
Right now I am just using my knowledge of other language and literally google “what loop powershell support?”, How to define a variable type eg. [int], operators(-and, -eq etc.), functions and all the others.
I am just hoping after doing this for a little and get more familiar, I can go back to the book and understand better. The book started out with Comlet, just a little on pipeline, then with some of the operators right at the beginning, I can’t follow without pre-knowledge on all those. So I am just hitting wild a little, look for stuffs that I know in other language. Mess with it a little than hopefully I can follow the books.
Do you have suggestion of a powershell book that has a lot of exercise so I can write programs and compare?
Anyway to attach a .docx here to show you what I wrote so far.
PS C:\Users\richm> help about_operat*
Name Category Module Synopsis
about_Operators HelpFile
about_Operator_Precedence HelpFile
The “PowerShell in Depth” and “PowerShell in Action” are both available in downloadable versions, readable in apps like Kindle, or in PDF version. If you bought the books on-line, go back to the vender site and see if you can add the downloads to your order, or cancel the order and order again. The downloadable versions are usually less expensive and sometimes are included with a paper copy at a small additional charge. Books from free eBook offer often include a downloadable version, but require you to register the paper version.
I like paper books, I like to right on them, make notes, red line and all. If the book is big, I actually cut them to thinner ones, staple them so I can carry them out all over the place reading.
You don’t want to see my books after I studied them.
Besides, I bought books used, these two books costed me less than $40 including shipping.