0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 42min

0 / 7 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete

PowerShell Variables Solution

Saving Progress...

In this lecture, I am going to give you the solution to the Variables Challenge that I gave you earlier.

So, If you haven’t completed the challenge just yet, and you don’t want to see the solution go ahead and pause this video, complete the challenge, and then come back because I am going to show you all the code that you need in order to finish this challenge.

I am going to bring up the PowerShell ISE.

Now, this is the code that I used to accomplish the challenge or complete the challenge that I gave you earlier.

So, the first thing I wanted you to do is to store the users First and Last names in two separate variables.

We can see here that we have the $firstName and $lastName variables and I am using the Read-Host to get the user's input to allow them to input whatever they want to enter for their First and Last name.

So, I have “Please enter your first name” and I have -Prompt. Again, below I did not use -Prompt but still accomplishes the same thing. You can probably look at this as being more efficient yet typing fewer commands is probably the best way to do it. I just wanted to show you both so you can be aware you could do both.

The next thing that I specified was that you need to create a new text file under the C:\VariableChallenge folder, and that’s done with the Set-Content command with the -Path argument. So, we have C:\VariableChallenge\ and then the file name.

So, for the file name, I wanted you to specify with today’s date - firstName - LastName .txt. If you remember that today was supposed to be specified in the following format: 2 digit day, 2 digit month, and then the 4 digit year - hours, minutes, and seconds. So, I was able to accomplish that using the Get-Date command and I stored the variable here called $today.

Next, inside of that file I wanted you to use a long date, and I was also able to accomplish this using the Get-Date and -Format with “F”. Now you might be wondering, how was I able to do that?  Well, if we run Get-Help Get-Date -Detailed and press Enter.

We are going to get all this information on how to use Get-Date and how to format this Get-Date. But it does not give you complete information, it just tells you how you might want to use it. I was able to construct the format “ddMMyyyy-HHmms” using the following web page:


This is what I was able to use to create these formats. So, I used “F” to get the full date and long time, and this is what I wanted you to include inside of the text file. Next, I was able to use the months, the year, and I can find down through all this information and get whatever I need to do.

If we run the Get-Date -Format “F” command and press Enter I can build the format that I want.

If I add “dd '' and press Enter I can start to build the format. If I add “ddMM” and “ddMMyyyy” and if you look at that web page I’ll find that to get the 4 digit year I need to use “yyyy”. 

Then I can use “:” and start specifying the time “ddMMyyyy:HHmms” and that’s the date.

This is how I formatted today’s date. Then, I just used -  and I included a $firstName variable that we stored at the beginning of the script and $lastName and .txt.

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Notify of
Inline Feedbacks
View all comments