0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 56min

0 / 8 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

PowerShell Modules

• 58min

0 / 7 lessons complete

Powershell Challenges

• 1hr 55min

0 / 23 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete

PowerShell Variables Solution

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

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:

https://docs.microsoft.com/en-us/dotnet/api/system.globalization.datetimeformatinfo?redirectedfrom=MSDN&view=net-6.0#format-strings-and-datetimeformatinfo-properties

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

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar