A question on explaining some PowerShell Syntax

Hi,

I was studying some PowerShell from the following site:

https://technet.microsoft.com/en-us/library/ff730960.aspx

------------------------------------

$a = Get-Date "Date: " + $a.ToShortDateString() "Time: " + $a.ToShortTimeString()

------------------------------------------------------

I understand some of this..  I tried to create my own function called time

-------------

 function time {$a=ToShortTimeString()}

-------------

This did not work I was able to create my function by doing the following:

------------------------------

function time {"Time: " + $a.ToShortTimeString()}

PS C:\Users\Administrator.BWCAT> time
Time: 2:42 PM

-------------------------------

I thought "ToShortTimeString()" was a method  however, am i wrong?  is the entire  "Time: " + part of that method also?

It does not seem to work without the "Time: " +    so I am just trying to understand the syntax I am using... Its been awhile since I had a .Net programming class and so even the word method is a little vague as I always got method and function confused a bit, but I think I have a vague general idea of method.

Anyway can someone break down in easy to understand perhaps using analogy of the syntax and why its all required so I can understand it?

function time {"Time: " + $a.ToShortTimeString()}

Thanks

February 17th, 2015 4:09pm

ToShortTimeString() is a method but it's part of the $a variable you have. It doesn't run on its own. Type $a | get-member and you'll see that method is one of $a's members.

Additionally, in your example of "function time {$a=ToShortTimeString()}" your function is setting $a equal to a value (albeit an invalid one) and not actually returning any value or writing anything to the screen.

  • Marked as answer by wilder7bc 13 hours 31 minutes ago
Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 4:14pm

What is the first portion though.

"Time: " +      

is "Time: "  one thing and  the "+" another  whats going on there?  is the entire thing part of the method or am I taking the "Time: " and then connecting it with the "+" to the method   and if so why is it required and what are those individual syntax components?

February 17th, 2015 4:53pm

duh... ok I see now thats  just text..

"Time: "  and we are concatenating it with the "+"

so I understand that.  so let me change my question a bit.

Why does it have to have the text in order to output method?

I would think just doing 

function time {$a=ToShortTimeString()}

not write anything to the screen?   I would think that would output the same time to the screen when I call the function.

However, unless I messed up I seem to recall it did not give the time when I called the function.

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 4:59pm

You're just writing "Time: " plus the output of $a.ToShortTimeString() to the console. you could replace it with write-host "Time: $($a.ToShortString())" and get the same result.

That's right that you're writing "Time: " and then connecting the output of the method.

February 17th, 2015 5:00pm

nm....

When I ran the correct syntax it does work as I would expect:

-------------------

PS C:\Users\Administrator.BWCAT> function time {$a.ToShortTimeString()}
PS C:\Users\Administrator.BWCAT> time
2:42 PM
PS C:\Users\Administrator.BWCAT>

---------------------

I messed up because I put $a=  instead of  $a.   

so it does not suprise me and I do understand it.  

Thanks for the assistance and looking at my post.

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 5:04pm

When your learning new things and its very broad sometimes you cannot tell  a tree is a tree because of the size of the forest and over thinking it.  I have always over looked basic things due to searching for a more complex answer and then later I have that oh Duh! moment.

this all started because I was reading a post on Desired State Configuration and the guy gave an example of a function.

function {get-time}

However, when I tried to execute that example I found out that there did not seem to be a get-time cmdlet.  However then I started searching on how to tell the time and I ended up following the rabbit down the hole on some off topic smaller searches... 

lol

so then I started seeing some cool things from that technet library page and got more distracted but in the end atleast I now understand whats going on there and feel much better as not understanding drives me crazy.

Regardless its educating so thanks.

February 17th, 2015 5:11pm

ToShortTimeString() is a method but it's part of the $a variable you have. It doesn't run on its own. Type $a | get-member and you'll see that method is one of $a's members.

Additionally, in your example of "function time {$a=ToShortTimeString()}" your function is setting $a equal to a value (albeit an invalid one) and not actually returning any value or writing anything to the screen.

  • Marked as answer by wilder7bc Tuesday, February 17, 2015 9:59 PM
Free Windows Admin Tool Kit Click here and download it now
February 18th, 2015 12:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics