Level 1
0 / 100 XP

Writing the New-Greeting Function

Click the dropdown below to see the full source code of the New-Greeting.ps1 file.

New-Greeting.ps1 Source Code

Javascript
# Function: New-Greeting # Description: Returns a random greeting message. function New-Greeting { # Delcare message greetings $greetings = @( "PowerShell modules are very cool!", "I am glad you are learning about PowerShell modules!", "PS Modules are the best!" ) # Get a random message $greeting = $greetings | Get-Random # Return the greeting return $greeting }

All right. In this lesson, we're going to be writing the functions that we'll be using for our custom module. Now, if you remember the last lecture, we created these files new greeting. ps1 and get greetingmessage. ps1. Now, you'll notice there's some placeholder text in here. You can download this. Under the resources of this lesson, it's a file called greetme-starter.Zip. This kind of includes just a description, and all of these files have some type of starter text. So does this greetme. psd1, and most usefully, they have this link to the documentation as far as, like, creating a PowerShell module manifest.

So if you want to get that, you can if you don't, that's fine. You can just look at the screen and create the functions as you see them here. But the purpose of this function is going to return a random greeting message. That's what we're going to do with this function. And the function name is going to be new dash greeting.

Okay. So I'm going to go ahead and get started and again, we want to keep this example as simple as possible. So I don't want to overwhelm you with super complicated scripts or functions. All we're going to do is create three lines of text that one of them is going to be PowerShell modules are cool, and then another one's like, I'm glad you're learning PowerShell. And…